为什么我的代码不工作?
问题描述:
tkinter的新功能,但截至目前,我不知道为什么我的代码不断返回失败,而不是通过。为什么我的代码不工作?
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()
答
它不工作,因为你需要使用以下方法来获取密码的输入值:
self.pass_entry.get()
因此,你应该有:
if self.entry.get() and self.pass_entry.get() == "hello":
作为一个边注意。如果你有密码输入控件,更好地做到这一点,如下所示:
self.pass_entry = tk.Entry(self, show="*")
试试'如果self.pass_entry.get()==“你好”:...'为'self.pass_entry'永远等于' 'hello'(前者是一个tkinter小部件 - 后者是一个字符串)。 – mgilson 2015-02-12 06:40:40