標籤:
python 圖形編程 from Tkinter import *root=Tk()root.title("my test")root.mainloop() #主迴圈以上是建立一個空白的表單====================================== button1=Button(root,text="button1")button2=Button(root,text="button2")button3=Button(root,text="button3")text = Entry(root)listbox = Listbox(root) button1.pack()button2.pack()button3.pack()text.pack()listbox.pack()root.mainloop() 以上是在表單中放置一些小的控制項=======================================def Button1(): listbox.insert(END, "button1 pressed")def Button3(): text_contents = text.get() listbox.insert(END, text_contents) text.delete(0,END)button1 = Button(root, text="button1", command = Button1)button3 = Button(root, text="button3", command = Button3)以上是按button1和button3時會出現相應的操作,也就是各控制項之間的互動.=============================================== scrollbar = Scrollbar(root, orient=VERTICAL)listbox = Listbox(root,yscrollcommand=scrollbar.set)scrollbar.configure(command=listbox.yview)scrollbar.pack()當listbox滿的時候,不會看到.加上捲軸後,可以看到.================================================= 安置視窗小組件將視窗分成兩部分,一部分是文字框和按鈕,另一部分控制列表框。將使用架構視窗小組件來實現這個功能,架構視窗小組件看上去像個容器,可以將其它的視窗小組件放在它的裡面--就像根視窗一樣。現在來建立一對架構 textframe = Frame(root) listframe = Frame(root) 我們已經將每個視窗小組件的所有者都設定為根視窗,接下來,我們改變它的所有者並應用到我們的新設計中:button1 = Button(textframe, text="button1", command = Button1)button2=Button(textframe,text="button2")button3 = Button(textframe, text="button3", command = Button3)text = Entry(textframe) scrollbar = Scrollbar(listframe, orient=VERTICAL)listbox = Listbox(listframe,yscrollcommand=scrollbar.set)scrollbar.configure(command=listbox.yview) 其實,這樣不會起太多作用,除非我們告訴Tk視窗小組件在架構中放置的位置,為實現這個我們可以使用包方法和三個關鍵字:side、fill和expand。在文文書處理器中你能把邊認為與對準線相似,它告訴視窗小組件架構它們一般需要設定的哪一邊,是頂部,底部,左邊還是右邊——預設的形式是群圍繞著中心這種方式。在這種情況下,除了用作左邊對準器的捲軸外,我們需要將其它的每件東西用捲軸包圍列表框的右邊。 第二個選項,fill,如果它增長了就告訴Tk哪個方向溢出了小視窗組件,在X軸方向(水平方向),Y軸方向(垂直方向)或者兩個方向都有。一般來說,我們能很好處理這些按鈕,但是它希望文字框和列表框能使用盡量多的空間——所以設定文字框在X軸方向溢出和列表框在兩個方向溢出——捲軸應該重新設定列表框的大小,所以設定它在Y軸方向溢出。 最後,expand選項告訴視窗小組件如果可能是否要擴張到免費的空間——增加expand=1這條語句到文字框和列表框包選項中就能實現這個效果。現在,你的代碼應該看上去像下面這樣: text.pack(side=LEFT, fill=X, expand=1) button1.pack(side=LEFT) button2.pack(side=LEFT)button3.pack(side=LEFT)listbox.pack(side=LEFT,fill=BOTH, expand=1)scrollbar.pack(side=RIGHT, fill=Y) 目前,所有的左邊按鈕像打包到其他的視窗小組件一樣,打包到兩個架構中並放置到根視窗裡面。記住,這種方式架構應該要擴張:textframe.pack(fill=X)listframe.pack(fill=BOTH,expand=1)========================================================== 鍵盤事件和滑鼠事件 我們有一些工作,或多或少的——但是它的介面一直有點不實用。如果我們能使用滑鼠和鍵盤使事情更直接的話,這些工作看上去將會更容易處理。當你們運行Tk主迴圈,在鍵盤上敲擊一個鍵或者四處移動滑鼠產生事件,你可以以同樣的方法綁定回叫訊號函數到按鈕上,這樣按鈕被按下去的時候也會產生相應事件。現在我們已經有個函數處理這樣的情況了,那就是按鈕3的回叫訊號,但是不幸地是,我們現在還不能使用,因為按鈕的事件回叫訊號與滑鼠和鍵盤不一樣。我們必須把它限制在其它的函數中: def ReturnInsert(event): Button3() 然後我們註冊這些事件的回叫訊號,並尋找使用綁定函數: text.bind("<Return>", ReturnInsert) 這裡我們使用<Return>事件代碼而不使用<Enter>事件代碼,這點是非常重要的,因為當滑鼠進入列表框時就會觸發第二個觸發器。現在我們想讓使用者使用右擊來從列表框中移動項目,對於相同的處理這種處理是相當棒的。首先我們寫一個回叫訊號作為輸入來接收事件: def DeleteCurrent(event): listbox.delete(ANCHOR) 然後我們綁定事件到這個回叫訊號上: listbox.bind("<Double-Button-3>", DeleteCurrent) 滑鼠右鍵在Tk中被稱為按鈕-3(不要與我們第三個形式按鈕回叫訊號的名字混淆了),因為第二個滑鼠按鍵涉及到滑鼠的中間鍵。最後,以防萬一他們想修改它,我們可以允許使用者拷貝一個易事貼返回到文字框。我們沒有一個函數能實現這個功能,所以我們將不得不在回叫訊號中寫一些新的代碼: def CopyToText(event): text.delete(0,END) current_note = listbox.get(ANCHOR) text.insert(0, current_note) 然後像先前那樣將事件綁定到回叫訊號中: listbox.bind("<Double-Button-1>", CopyToText) 你不僅僅可以限制這些事件,如果你想知道更多的關於哪些事件你能綁定的話,請查看Tk內部庫介紹。 現在看上去是整理我們程式的時候了。按鈕1對於一個函數來說是一個不太好的名字——曾經有相似的名字使我們陷入困境,在這裡我們就不要重蹈覆轍了。我們也有機會改變一些按鈕作用,比如,我們可以設定輸入按鈕來關閉文字框,按鈕1沒有特別的作用,所以我們可以除去它了。修改這些並沒有真正地功能性改變,但是程式現在看上去像這樣: #!/usr/bin/python from Tkinter import * root = Tk() root.geometry("600x400") root.title("Note Taker") def Enter(): text_contents = text.get() listbox.insert(END, text_contents) text.delete(0,END) def Remove(): listbox.delete(ANCHOR) def Save(): pass def ReturnInsert(event): Enter() def DeleteCurrent(event): Remove() def CopyToText(event): text.delete(0, END) current_note = listbox.get(ANCHOR) text.insert(0, current_note) textframe = Frame(root) listframe = Frame(root) enter_button = Button(textframe, text="Enter", command = Enter) remove_button = Button(textframe, text="Remove", command = Remove) save_button = Button(textframe, text="Save", command = Save) text = Entry(textframe) scrollbar = Scrollbar(listframe, orient=VERTICAL) listbox = Listbox(listframe, yscrollcommand=scrollbar.set, selectmode=EXTENDED) scrollbar.configure(command=listbox.yview) text.bind("<Return>", ReturnInsert) listbox.bind("<Double-Button-3>", DeleteCurrent) listbox.bind("<Double-Button-1>", CopyToText) text.pack(side=LEFT, fill=X, expand=1) enter_button.pack(side=LEFT) remove_button.pack(side=LEFT) save_button.pack(side=LEFT) listbox.pack(side=LEFT,fill=BOTH, expand=1) scrollbar.pack(side=RIGHT, fill=Y) textframe.pack(fill=X) listframe.pack(fill=BOTH, expand=1) root.mainloop()=================================================== 儲存會話間的資料可以通過儲存列表的內容到一個檔案中,同時在程式啟動並執行時候下載它來安裝這個程式。為實現這個我們將使用pickle模組——Python的系列版本,或者使用集結待發的資料類型到檔案中。首先我們需要輸入模組:import pickle然後我們得到一個變數,易事貼,這個變數包含了易事貼列表。為了將它儲存到一個檔案中,我們僅僅需要開啟檔案寫入,並使用dump函數。你可能會注意到第三個按鈕的回叫訊號是空的,讓我們來改變它以便將當前的易事貼列表儲存到檔案中:def Save():f = file("notes.db", "wb")notes = listbox.get(0, END)pickle.dump(notes, f)現在,當按下“儲存”按鈕,我們可以在案頭上得到我們的易事貼的副本。這個不會給我們太多的協助,除非我們有一些下載這些易事貼的方法,所以讓我們在啟動主迴圈前正確地把列表框填滿:try:f = file("notes.db", "rb")notes = pickle.load(f)for item in notes:listbox.insert(END,item)f.close()except:pass我們需要用一個try語句將所有的東西捆綁起來,以防檔案開啟拋出一個異常,也就是說,如果檔案“notes.db”不存在的話,應該拋出一個異常,產生警告。然而,如果一個異常被拋出,我們不會真正的注意到它,我們只是僅僅不會下載任何易事貼到列表中而已。#!/usr/bin/pythonfrom Tkinter import *import pickleroot = Tk()root.geometry("600x400")root.title("Note Taker")def Enter(): text_contents = text.get() listbox.insert(END, text_contents) text.delete(0,END)def Remove(): listbox.delete(ANCHOR)def Save(): f=file("d:\\python-std\\notes.db","w") notes=listbox.get(0,END) pickle.dump(notes,f)def ReturnInsert(event): Enter()def DeleteCurrent(event): Remove()def CopyToText(event): text.delete(0, END) current_note = listbox.get(ANCHOR) text.insert(0, current_note) textframe = Frame(root)listframe = Frame(root) enter_button = Button(textframe, text="Enter", command = Enter)remove_button = Button(textframe, text="Remove", command = Remove)save_button = Button(textframe, text="Save", command = Save) text = Entry(textframe)scrollbar = Scrollbar(listframe, orient=VERTICAL)listbox = Listbox(listframe, yscrollcommand=scrollbar.set, selectmode=EXTENDED)scrollbar.configure(command=listbox.yview) text.bind("<Return>", ReturnInsert)listbox.bind("<Double-Button-3>", DeleteCurrent)listbox.bind("<Double-Button-1>", CopyToText) text.pack(side=LEFT, fill=X, expand=1)enter_button.pack(side=LEFT)remove_button.pack(side=LEFT)save_button.pack(side=LEFT)listbox.pack(side=LEFT,fill=BOTH, expand=1)scrollbar.pack(side=RIGHT, fill=Y) try: f=file("d:\\python-std\\notes.db","rb") notes=pickle.load(f) for item in notes: listbox.insert(END,item) f.close()except: pass textframe.pack(fill=X)listframe.pack(fill=BOTH, expand=1) root.mainloop()
python圖形編程