瞬态输入window

Transient input window

我有一个系统,其中有一个弹出窗口 window 请求用户输入,然后 return 将输入输入到代码主体进行处理。我有 window 正确弹出,使用 transient 确保它保持在顶部,但是我找不到将 window return 数据制作到调用它的地方的方法。 当前:

from tkinter import *

class loginwindow(object):
    def __init__(self, parent):
        self.parent=parent
        self.data=None #Default value, to say its not been set yet
        self.root=Toplevel(self.parent)
        self.root.transient(self.parent)
        self.username=Entry(self.root)
        self.password=Entry(self.root, show="*")
        self.ok=Button(self.root, text="Continue", command=self.checkPass); self.ok.grid(row=5, column=2, sticky="ew")
        self.cancel=Button(self.root, text="Cancel", command=self.cancelPass); self.cancel.grid(row=5, column=1, sticky="ew")


    def checkPass(self):
        self.data=(self.username.get(), self.password.get())
        self.root.quit()

    def cancelPass(self):
        self.data=False
        self.root.quit()

parent=Tk()
passWindow=loginwindow(parent)
#....? how do I get passWindow.data from the window, considering it will only be defined 
#to be something besides None once the user has clicked the continue button

我已经尝试循环等待 passWindow.data 的值从 None 改变,但这会导致 loginwindow 不出现,或者脚本锁定。理想情况下,我想将 transient 保留在那里,因为它可以防止用户点击 window,但我知道 transient 是 window 没有出现的原因(没有)

有什么想法吗?

您有点想将两个问题合二为一,一个是关于将值从弹出窗口 window 传递到主窗口 window,一个是关于 transient 无法正常工作。我对第一个问题的回答如下,对于第二个问题,我真的不知道你遇到的问题是什么。据我所知,transient 按照您的使用方式正常工作。


您可以使用 wait_window 让 Tkinter 主循环等待 window 关闭后再继续。如果将弹出 window 创建放在父循环启动后调用的函数中,则可以使用 passWindow.wait_window() 来“暂停”函数的进一步执行,然后再继续执行其余行。在 window 关闭后,可以在 passWindow.data

中找到所需的数据
class loginwindow(object):
    def __init__(self, parent):
        self.parent=parent
        self.data=None #Default value, to say its not been set yet
        self.root=Toplevel(self.parent)
        self.root.transient(self.parent)
        self.username=Entry(self.root); self.username.pack()
        self.password=Entry(self.root, show="*"); self.password.pack()
        self.ok=Button(self.root, text="Continue", command=self.checkPass); self.ok.pack()
        self.cancel=Button(self.root, text="Cancel", command=self.cancelPass); self.cancel.pack()

    def checkPass(self):
        self.data=(self.username.get(), self.password.get())
        self.root.destroy()

    def cancelPass(self):
        self.data=False
        self.root.destroy()


def popup():
    passWindow=loginwindow(parent)
    passWindow.parent.wait_window(passWindow.root)
    print passWindow.data

parent=Tk()
Button(parent, text='popup', command=popup).pack()
parent.mainloop()