如何获得Tkinter入门?

时间:2012-11-04 10:40:07

标签: python tkinter

在这种情况下,我将如何实施“OnButtonClick”,以便在按下按钮时打印“e1”的用户输入?

from Tkinter import *

class App:    
    def __init__(self, master):
        e1 = Entry(master)
        e1.grid(row=1, column=0, sticky=N)
        Button(master, text="Start", command=self.OnButtonClick).grid(row=4, column=0)

    def OnButtonClick(self):



if __name__ == "__main__":
    root = Tk()
    app = App(root)
    root.mainloop()

1 个答案:

答案 0 :(得分:1)

您需要在App上存储对entry实例的引用:

class App:    
    def __init__(self, master):
        self.e1 = Entry(master)
        self.e1.grid(row=1, column=0, sticky=N)
        Button(master, text="Start", command=self.OnButtonClick).grid(row=4, column=0)

现在,您可以从self.e1访问OnButtonClick并将其命名为s。get()`方法以获取当前条目文字:

    def OnButtonClick(self):
        value = self.e1.get()