问题描述
限时送ChatGPT账号..我正在尝试实现一个获取用户密码的对话框.我创建了从 tk.Toplevel
继承的 PasswordDiaglog
类,但这会导致它的执行不阻塞父框架的问题.
I am attempting to implement a dialog that gets the users password. I have created the class PasswordDiaglog
that inherits from tk.Toplevel
but this causes the problem that it's execution is non-blocking of the parent frame.
import Tkinter as tk
class PasswordDialog(tk.Toplevel):
def __init__(self, parent):
tk.Toplevel.__init__(self)
self.password = None
self.entry = tk.Entry(self, show='*')
self.entry.pack()
self.button = tk.Button(self)
self.button["text"] = "Submit"
self.button["command"] = self.StorePass
self.button.pack()
def StorePass(self):
self.password = self.entry.get()
self.destroy()
print '1: Password was', self.password
class MainApplication(tk.Frame):
def __init__(self, root):
tk.Frame.__init__(self, root)
self.button = tk.Button(self)
self.button["text"] = "Password"
self.button["command"] = self.GetPassword
self.button.pack()
def GetPassword(self):
passwd = PasswordDialog(self)
# HALT HERE
print '2: Password was', passwd.password
if __name__ == "__main__":
root = tk.Tk()
MainApplication(root).pack(side="top", fill="both", expand=True)
root.mainloop()
通过运行我的代码,将提交的 foobar
作为密码,在终端中可以看到以下输出:
By running my code a submitting foobar
as the password the following output is seen in the terminal:
2: Password was None
1: Password was foobar
预期的输出应该是:
1: Password was foobar
2: Password was foobar
关于如何解决这个问题或如何实现密码对话框的任何想法?
Any ideas on how to solve this or how to implement a password dialog in general?
在输入 entry
后按回车键也可以调用 StoredPass()
.
It would also be nice to call StoredPass()
by hitting return after typing in the entry
.
推荐答案
将密码存储为 MainAplication
类的属性,并使用传入的 parent
作为句柄PasswordDialog
类意味着你可以使用 self.wait_window(PasswordDialog(self))
来阻止执行,直到 PasswordDialog
被销毁:
Storing the password as attribute of the MainAplication
class and using the passed in parent
as the handle from the PasswordDialog
class means you can use self.wait_window(PasswordDialog(self))
to block the execution until the PasswordDialog
is destroyed:
import Tkinter as tk
class PasswordDialog(tk.Toplevel):
def __init__(self, parent):
tk.Toplevel.__init__(self)
self.parent = parent
self.entry = tk.Entry(self, show='*')
self.entry.bind("<KeyRelease-Return>", self.StorePassEvent)
self.entry.pack()
self.button = tk.Button(self)
self.button["text"] = "Submit"
self.button["command"] = self.StorePass
self.button.pack()
def StorePassEvent(self, event):
self.StorePass()
def StorePass(self):
self.parent.password = self.entry.get()
self.destroy()
print '1: Password was', self.parent.password
class MainApplication(tk.Frame):
def __init__(self, root):
tk.Frame.__init__(self, root)
self.password = None
self.button = tk.Button(self)
self.button["text"] = "Password"
self.button["command"] = self.GetPassword
self.button.pack()
def GetPassword(self):
self.wait_window(PasswordDialog(self))
print '2: Password was', self.password
if __name__ == "__main__":
root = tk.Tk()
MainApplication(root).pack(side="top", fill="both", expand=True)
root.mainloop()
现在输出符合预期:
1: Password was foobar
2: Password was foobar
<小时>
要绑定回车键,您可以使用:
To bind the Return key you can use:
self.entry.bind("<KeyRelease-Return>", self.StorePassEvent)
带有包装函数:
def StorePassEvent(self, event):
self.StorePass()
您也可以使用 lambda
代替:
You can also use lambda
instead:
self.entry.bind("<KeyRelease-Return>", lambda x: self.StorePass())
这篇关于使用 Tkinter 实现密码对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论