from Tkinter import *
from tkMessageBox import *
class Gui:
def __init__(self, root):
self.container = Frame(root)
self.container.grid()
self.inputText = Text(self.container, width=50, height=8)
self.outputText = Text(self.container, width=50, height=8, bg='#E0E0E0', state=DISABLED)
self.inputText.grid(row=0, column=0)
self.outputText.grid(row=0, column=1)
self.inputText.bind("<Key>", self.translate)
def translate(self, event):
input = self.inputText.get(0.0, END)
output = self.outputText.get(0.0, END)
self.outputText.config(state=NORMAL)
self.outputText.delete(0.0, END)
self.outputText.insert(INSERT, input)
self.outputText.config(state=DISABLED)
showinfo(message="Input: %s characters\nOutput: %s characters" % (len(input), len(input)))
root = Tk() #toplevel object
app = Gui(root) #call to the class where gui is defined
root.mainloop() #enter event loop
在tkinter中處理gui我對於事件處理程序運行的順序有點困惑。如果你運行上面的代碼,你會希望看到...python gui events out of order
1)編輯文本小部件會觸發事件處理程序,但它似乎沒有註冊實際更改而關閉它, 2)即使當文本小部件清除(即繼續按BackSpace),它似乎仍然有一個字符長度的字符串, 3)輸出小部件只在NEXT事件觸發器被觸發時才接收其更新,儘管事實上數據出現在上一個事件上。
這只是如何綁定tkinter工作或我在這裏失蹤的東西?
更新所述輸入窗口小部件時,我想的行爲是: 1)顯示的變化, 2)輸入的事件處理程序, 3)更新輸出插件, 4)顯示消息框。
感謝您抽出寶貴的時間輸入Bryan,很多人解釋說。 – dave 2010-03-21 14:17:50