如何正确处理确认事件?

时间:2012-11-26 13:42:33

标签: python oop tkinter

我正在尝试创建具有2个输入区域(用于登录和密码)和确认按钮的框架(按下此按钮后 - 代码将读取区域值)。但是我不知道如何在类App内完成它而不使用某些全局函数。

from Tkinter import *

class App:

    def __init__(self, master):

        frame_credentials = Frame(master, width=100, height=200)
        frame_credentials.pack()

        self.label_login = Label(frame_credentials, text='login')
        self.text_login = Entry(frame_credentials, width=15)
        self.label_pass = Label(frame_credentials, text='password')
        self.text_pass = Entry(frame_credentials, show="*", width=15)
        self.button_ok = Button(frame_credentials, text="Login")

        self.label_login.grid(row=0, column=0)
        self.text_login.grid(row=1, column=0)
        self.label_pass.grid(row=2, column=0)
        self.text_pass.grid(row=3, column=0)
        self.button_ok.grid(row=0, column=1, rowspan=4)
        self.button_ok.bind("<Button-1>", enter_in)

    def enter_in(self):
        print self.text_login, self.text_pass

root = Tk()

app = App(root)

root.mainloop()

1 个答案:

答案 0 :(得分:0)

不要绑定到<Button-1>;相反,使用command属性并为其指定对象中方法的名称。例如:

class App:
    def __init__(...):
        ...
        self.button_ok = Button(..., command=self.enter_in)
        ...
    def enter_in(self):
        <put your login logic here>