问题描述
我正在为一个正在进行的小项目编写gui,这需要我有多个页面,可以在多个页面之间进行切换。我使用类(如下所示)来设置这些页面。现在,由于我还不熟悉OOP,并且对于Tkinter和Python来说还很陌生,所以我现在很难从输入框中获取数据。
from tkinter import *
#Create login screen
class login(Frame):
def __init__(self,*args,**kwargs):
Frame.__init__(self,**kwargs)
entry_password = StringVar(self)
entry_label = Label(self,text="Enter Password").place(relx = 0.5,rely=0.45,anchor="center")
entry_Box = Entry(self,textvariable=entry_password)
entry_Box.place(relx = 0.5,rely=0.5,anchor="center")
submit_button = Button(self,text="Submit",command=print(entry_password.get())).place(relx = 0.5,rely=0.55,anchor="center")
#Create password list screen
class password_list(Frame):
def __init__(self,**kwargs)
test = Label(self,text="Password list")
test.pack(side="top",fill="both",expand=True)
#Create main frame
class main(Frame):
def __init__(self,**kwargs)
buttonframe = Frame(self)
buttonframe.pack(side="top",fill="x",expand=False)
container = Frame(self)
container.pack(side="top",expand=True)
login_screen = login(self)
login_screen.place(in_=container,x=0,y=0,relwidth=1,relheight=1)
login_screen.lift()
password_list_screen = password_list(self)
password_list_screen.place(in_=container,relheight=1)
Button1 = Button(buttonframe,text="Lift Password list",command=password_list_screen.lift)
Button2 = Button(buttonframe,text="Lift Login screen",command=login_screen.lift)
Button1.pack(side="left")
Button2.pack(side="left")
if __name__ == "__main__":
root = Tk()
main_screen = main(root)
main_screen.pack(side="top",expand=True)
root.wm_geometry("1200x700")
root.mainloop()
当我运行程序时,在输入框中输入文本,然后按一下按钮,它不会打印任何内容。如何正确地从输入框中检索数据?我需要它来验证它作为解密文件的密码。
解决方法
您可以使用'.get'。
v = StringVar()
e = Entry(master,textvariable=v)
e.pack()
v.set("a default value")
s = v.get()
您可以在documentation上查看更多内容。
,我想出了一种方法,可以通过一部非常出色的youtube视频来修复它。我只需要更改一行:
submit_button = Button(self,text="Submit",command=lambda: submit(entry_password)).place(relx = 0.5,rely=0.55,anchor="center")
我不太明白为什么lambda可以完成这项工作,但是它可以...有人可以向我解释吗?