tkinter新手,但截至目前,我不知道为什么我的代码会一直返回Failed而不是传递。
import tkinter as tk
class GUI(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.entry = tk.Entry(self)
self.user_Label = tk.Label(self, text="Username")
self.pass_entry = tk.Entry(self)
self.pass_Label = tk.Label(self, text="Password")
self.login = tk.Button(self, text="Login", foreground="black", command=self.on_button)
#Packing
self.user_Label.pack()
self.entry.pack()
self.pass_Label.pack()
self.pass_entry.pack()
self.login.pack()
def on_button(self):
if self.entry and self.pass_entry == "hello":
print("passed")
else:
print("Failed")
app = GUI()
app.mainloop()
答案 0 :(得分:1)
它不起作用,因为您需要使用以下内容来获取输入密码的值:
self.pass_entry.get()
因此,您应该:
if self.entry.get() and self.pass_entry.get() == "hello":
作为旁注。如果您有密码Entry小部件,最好按如下方式执行:
self.pass_entry = tk.Entry(self, show="*")