用于检查条目的 Tkinter 按钮
Tkinter Button to check entries
我正在尝试创建一个按钮来检查条目中的信息。如果信息为 True,则 window 应该被清除并显示两个新标签,否则新的 window 应该弹出并显示一条错误消息。目前我无法弄清楚为什么代码每次都在 else 块中结束,无论我在条目中键入什么。有人能够解释为什么以及如何解决这个问题吗?
也许“Entry.get()”方法与我预期的不同?..
from tkinter import *
class FirstPrototype:
def __init__(self, top=None):
self.top = top
top.title("Prototype")
top.configure(bg="black", highlightbackground="grey", highlightcolor="darkgrey")
wWidth = top.winfo_reqwidth()
wHeight = top.winfo_reqheight()
x = int(top.winfo_screenwidth() / 2 - (wWidth / 2))
y = int(top.winfo_screenheight() / 3 - (wHeight / 2))
top.geometry(f"+{x}+{y}")
top.minsize(300, 132)
self.Account = Button(text="Account", command=self.log_reg).pack()
def log_reg(self):
self.clear_window()
Label(text="ID:", bg="darkgrey").pack(fill=X)
e1 = Entry()
e1.pack(fill=X)
Label(text="PW:", bg="darkgrey").pack(fill=X)
e2 = Entry()
e2.pack(fill=X)
Button(text="Login", command=self.check_id).pack(fill=X)
Button(text="Register").pack(fill=X)
return e1, e2
def clear_window(self):
for widget in top.winfo_children():
widget.destroy()
def check_id(self):
e1, e2 = self.log_reg()
u_id = e1.get()
u_pw = e2.get()
if u_id == "Qwe123" and u_pw == "Qwe123":
self.clear_window()
Label(top, text="Nickname:", bg="darkgrey").pack(fill=X)
Label(top,text="Examplenickname",bg="darkgrey").pack(fill=X)
else:
error_w = Tk()
error_w.title("ERROR")
Label(error_w, text="ERROR").place(relx=0.5, rely=0.5, anchor=CENTER)
top = Tk()
exa_gui = FirstPrototype(top)
top.mainloop()
解释请参考我的评论
这里:
from tkinter import *
class FirstPrototype:
def __init__(self, top=None):
self.top = top
top.title("Prototype")
top.configure(bg="black", highlightbackground="grey", highlightcolor="darkgrey")
wWidth = top.winfo_reqwidth()
wHeight = top.winfo_reqheight()
x = int(top.winfo_screenwidth() / 2 - (wWidth / 2))
y = int(top.winfo_screenheight() / 3 - (wHeight / 2))
top.geometry(f"+{x}+{y}")
top.minsize(300, 132)
self.Account = Button(text="Account", command=self.log_reg).pack()
self.e1 = None
self.e2 = None
def log_reg(self):
#self.clear_window()
Label(text="ID:", bg="darkgrey").pack(fill=X)
self.e1 = Entry()
self.e1.pack(fill=X)
Label(text="PW:", bg="darkgrey").pack(fill=X)
self.e2 = Entry()
self.e2.pack(fill=X)
Button(text="Login", command=self.check_id).pack(fill=X)
Button(text="Register").pack(fill=X)
def clear_window(self):
for widget in top.winfo_children():
widget.destroy()
def check_id(self):
#e1, e2 = self.log_reg()
u_id = self.e1.get()
u_pw = self.e2.get()
print(u_id)
if u_id == "Qwe123" and u_pw == "Qwe123":
self.clear_window()
Label(top, text="Nickname:", bg="darkgrey").pack(fill=X)
Label(top,text="Examplenickname",bg="darkgrey").pack(fill=X)
else:
error_w = Tk()
error_w.title("ERROR")
Label(error_w, text="ERROR").place(relx=0.5, rely=0.5, anchor=CENTER)
top = Tk()
exa_gui = FirstPrototype(top)
top.mainloop()
我正在尝试创建一个按钮来检查条目中的信息。如果信息为 True,则 window 应该被清除并显示两个新标签,否则新的 window 应该弹出并显示一条错误消息。目前我无法弄清楚为什么代码每次都在 else 块中结束,无论我在条目中键入什么。有人能够解释为什么以及如何解决这个问题吗? 也许“Entry.get()”方法与我预期的不同?..
from tkinter import *
class FirstPrototype:
def __init__(self, top=None):
self.top = top
top.title("Prototype")
top.configure(bg="black", highlightbackground="grey", highlightcolor="darkgrey")
wWidth = top.winfo_reqwidth()
wHeight = top.winfo_reqheight()
x = int(top.winfo_screenwidth() / 2 - (wWidth / 2))
y = int(top.winfo_screenheight() / 3 - (wHeight / 2))
top.geometry(f"+{x}+{y}")
top.minsize(300, 132)
self.Account = Button(text="Account", command=self.log_reg).pack()
def log_reg(self):
self.clear_window()
Label(text="ID:", bg="darkgrey").pack(fill=X)
e1 = Entry()
e1.pack(fill=X)
Label(text="PW:", bg="darkgrey").pack(fill=X)
e2 = Entry()
e2.pack(fill=X)
Button(text="Login", command=self.check_id).pack(fill=X)
Button(text="Register").pack(fill=X)
return e1, e2
def clear_window(self):
for widget in top.winfo_children():
widget.destroy()
def check_id(self):
e1, e2 = self.log_reg()
u_id = e1.get()
u_pw = e2.get()
if u_id == "Qwe123" and u_pw == "Qwe123":
self.clear_window()
Label(top, text="Nickname:", bg="darkgrey").pack(fill=X)
Label(top,text="Examplenickname",bg="darkgrey").pack(fill=X)
else:
error_w = Tk()
error_w.title("ERROR")
Label(error_w, text="ERROR").place(relx=0.5, rely=0.5, anchor=CENTER)
top = Tk()
exa_gui = FirstPrototype(top)
top.mainloop()
解释请参考我的评论
这里:
from tkinter import *
class FirstPrototype:
def __init__(self, top=None):
self.top = top
top.title("Prototype")
top.configure(bg="black", highlightbackground="grey", highlightcolor="darkgrey")
wWidth = top.winfo_reqwidth()
wHeight = top.winfo_reqheight()
x = int(top.winfo_screenwidth() / 2 - (wWidth / 2))
y = int(top.winfo_screenheight() / 3 - (wHeight / 2))
top.geometry(f"+{x}+{y}")
top.minsize(300, 132)
self.Account = Button(text="Account", command=self.log_reg).pack()
self.e1 = None
self.e2 = None
def log_reg(self):
#self.clear_window()
Label(text="ID:", bg="darkgrey").pack(fill=X)
self.e1 = Entry()
self.e1.pack(fill=X)
Label(text="PW:", bg="darkgrey").pack(fill=X)
self.e2 = Entry()
self.e2.pack(fill=X)
Button(text="Login", command=self.check_id).pack(fill=X)
Button(text="Register").pack(fill=X)
def clear_window(self):
for widget in top.winfo_children():
widget.destroy()
def check_id(self):
#e1, e2 = self.log_reg()
u_id = self.e1.get()
u_pw = self.e2.get()
print(u_id)
if u_id == "Qwe123" and u_pw == "Qwe123":
self.clear_window()
Label(top, text="Nickname:", bg="darkgrey").pack(fill=X)
Label(top,text="Examplenickname",bg="darkgrey").pack(fill=X)
else:
error_w = Tk()
error_w.title("ERROR")
Label(error_w, text="ERROR").place(relx=0.5, rely=0.5, anchor=CENTER)
top = Tk()
exa_gui = FirstPrototype(top)
top.mainloop()