《Python程式設計》筆記(十一)

2021-07-16 12:51:48 字數 1826 閱讀 4451

常規繪製:

# 實際返回的id是數字

id = canvas.create_line(from_x, from_y, to_x, to_y)

id = canvas.create_oval(from_x, from_y, to_x, to_y)

id = canvas.create_arc(from_x, from_y, to_x, to_y)

id = canvas.create_rectangle(from_x, from_y, to_x, to_y)

id = canvas.create_image(x, y, image=img, anchor=s)

id = canvas.create_window(x, y, window=widget)

id = canvas.create_text(x, y, text='hello')

# 將物件偏移到指定位置

canvas.move(obj_or_id, off_x, off_y)

# 刪除物件

canvas.delete(obj_or_id)

# 將物件移動到前端

canvas.tkraise(obj_or_id)

#降低物件層次

canvas.lower(obj_or_id)

# 對畫布物件設定

canvas.itemconfig(obj_or_id, fill='red')

canvas.create_line(x, y, to_x, to_y, tag='line')

canvas.move('line', off_x, off_y)

canvas.addtag_withtag('test', id)

canvas.delete('test')

canvas.find_*方法可以查詢物件。

grid網格布局:這是另外一種常用的布局管理器。在同乙個toplevel或者root下,packgrid不能同時使用。

基礎的網格布局示例:

def

main

(): colors = ['red', 'green', 'yellow', 'blue', 'black']

for i, c in enumerate(colors):

label(text=c, width=35, relief=sunken).grid(row=i, column=0)

entry(bg=c, fg='white', width=50).grid(row=i, column=1)

mainloop()

rowspancolumnspan分別用於設定跨行和跨列的數量。

tkinter中提供的額外工具,用於安排延遲,同時又能夠更新螢幕:

在tkinter中使用多執行緒需要注意的是:只有主線程(建立gui並啟動mainloop()的執行緒)可以做gui呼叫。無論如何,多個執行緒都不應該同時更新gui。

按鈕元件的flash方法可以閃爍。元件的pack_forget方法可以擦除(取消對映)乙個繪製的元件,而後pack可以重現。此外,grid_forgetgrid方法也類似。

其他的高階元件:

Python核心程式設計學習筆記(十一)

31 range 內建函式 1 range 的完整語法,要求提供兩個或三個整數引數 range start,end,step 1 range 會返回乙個包含所有k的列表,這裡start k end,從start到end,k每次遞增step。step不可以為零,否則將發生錯誤。range 2,19,3...

Python學習筆記(十一) 函式式程式設計

一 函式式程式設計理念 函式式程式設計就是一種抽象程度很高的程式設計正規化,純粹的函式式程式語言編寫的函式沒有變數,因此,任意乙個函式,只要輸入是確定的,輸出就是確定的,這種純函式我們稱之為沒有 而允許使用變數的程式語言,由於函式內部的變數狀態不確定,同樣的輸入,可能得到不同的輸出,因此,這種函式是...

python筆記 十一

open file,mode r buffering 1,encoding none,errors none,newline none,closefd true,opener false mode r 唯讀 w 可寫 會清除檔案內容 a 附加資料 b 二進位制資料模式 x 新建乙個檔案 可寫 開啟檔...