【发布时间】:2020-12-01 09:40:47
【问题描述】:
我正在为我正在处理的一个小项目编写一个 gui,它需要我有多个可以在它们之间切换的页面。我使用类(如下所示)来设置这些页面。我现在很难从输入框中获取数据,因为我还不熟悉 OOP,而且对 Tkinter 和 Python 总体来说还很陌生。
from tkinter import *
#Create login screen
class login(Frame):
def __init__(self, *args, **kwargs):
Frame.__init__(self, *args, **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, *args, **kwargs):
Frame.__init__(self, *args, **kwargs)
test = Label(self, text="Password list")
test.pack(side="top", fill="both", expand=True)
#Create main frame
class main(Frame):
def __init__(self, *args, **kwargs):
Frame.__init__(self, *args, **kwargs)
buttonframe = Frame(self)
buttonframe.pack(side="top", fill="x", expand=False)
container = Frame(self)
container.pack(side="top", fill="both", 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, x=0, y=0, relwidth=1, 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", fill="both", expand=True)
root.wm_geometry("1200x700")
root.mainloop()
当我运行程序时,在输入框中输入文本并按下按钮,它不会打印任何内容。如何正确地从输入框中检索数据?我需要它来验证它作为解密文件的密码。
【问题讨论】:
-
entry_box.get()将保存您的输入框的值 -
既然你说你是 python 和 tkinter 的新手,我建议你先不使用类(它真的很容易),然后再实现类。但从困难开始也不错:D
标签: python user-interface tkinter