0

我正在尝试创建具有 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()
4

1 回答 1

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>
于 2012-11-26T16:29:10.383 回答