Python 入門指南 - 設定 command

Tk 的 GUI 事件 (event) 由替每個視窗元件設定 command , command 就是指定與該元件連動的方法 (method)




例如 GUIDemo 中有 NewLoadSaveEncodeDecodeClearCopy 等七個按鈕,我們替每個按鈕設計一個方法,使按下按鈕後就將 self.display 的文字標籤顯示按下哪個按鈕。程式如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from tkinter import *
 
class GUIDemo(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.grid()
        self.createWidgets()
 
    def createWidgets(self):
        self.inputText = Label(self)
        self.inputText["text"] = "Input:"
        self.inputText.grid(row=0, column=0)
        self.inputField = Entry(self)
        self.inputField["width"] = 50
        self.inputField.grid(row=0, column=1, columnspan=6)
 
        self.outputText = Label(self)
        self.outputText["text"] = "Output:"
        self.outputText.grid(row=1, column=0)
        self.outputField = Entry(self)
        self.outputField["width"] = 50
        self.outputField.grid(row=1, column=1, columnspan=6)
         
        self.new = Button(self)
        self.new["text"] = "New"
        self.new.grid(row=2, column=0)
        self.new["command"] =  self.newMethod
        self.load = Button(self)
        self.load["text"] = "Load"
        self.load.grid(row=2, column=1)
        self.load["command"] =  self.loadMethod
        self.save = Button(self)
        self.save["text"] = "Save"
        self.save.grid(row=2, column=2)
        self.save["command"] =  self.saveMethod
        self.encode = Button(self)
        self.encode["text"] = "Encode"
        self.encode.grid(row=2, column=3)
        self.encode["command"] =  self.encodeMethod
        self.decode = Button(self)
        self.decode["text"] = "Decode"
        self.decode.grid(row=2, column=4)
        self.decode["command"] =  self.decodeMethod
        self.clear = Button(self)
        self.clear["text"] = "Clear"
        self.clear.grid(row=2, column=5)
        self.clear["command"] =  self.clearMethod
        self.copy = Button(self)
        self.copy["text"] = "Copy"
        self.copy.grid(row=2, column=6)
        self.copy["command"] =  self.copyMethod
 
        self.displayText = Label(self)
        self.displayText["text"] = "something happened"
        self.displayText.grid(row=3, column=0, columnspan=7)
     
    def newMethod(self):
        self.displayText["text"] = "This is New button."
 
    def loadMethod(self):
        self.displayText["text"] = "This is Load button."
 
    def saveMethod(self):
        self.displayText["text"] = "This is Save button."
 
    def encodeMethod(self):
        self.displayText["text"] = "This is Encode button."
 
    def decodeMethod(self):
       self.displayText["text"] = "This is Decode button."
 
    def clearMethod(self):
       self.displayText["text"] = "This is Clear button."
 
    def copyMethod(self):
       self.displayText["text"] = "This is Copy button."
 
if __name__ == '__main__':
    root = Tk()
    app = GUIDemo(master=root)
    app.mainloop()
 
# 《程式語言教學誌》的範例程式
# 檔名:tkdemo3.py
# 功能:示範 Python 程式
# 作者:張凱慶
# 時間:西元 2012 年 12 月


設定 command 例如 New 按鈕,利用中括弧設定 "command" 值,等號右邊為處理按下 New 的方法
27
self.new["command"] =  self.newMethod


所以要加入 newMethod() 的定義,這裡是直接將 self.displayText 改成相對應的訊息
57
58
def newMethod(self):
    self.displayText["text"] = "This is New button."


其他六個按鈕亦同,請參考上面程式。執行後,我們點擊一下 Encode 按鈕



結果 ok !接下來,我們要開始整合 Encrypt 類別 (class) 囉!


中英文術語對照
事件event
方法method
類別class


您可以繼續參考
GUI 篇


相關目錄
回 Python 入門指南
回 Python 教材
回首頁


參考資料
http://docs.python.org/3.1/library/tk.html
http://docs.python.org/3.1/library/tkinter.html

沒有留言: