隐藏Tkinter根窗口,同时显示模态窗口

时间:2014-09-24 23:55:35

标签: python tkinter ttk

我有一个基于TKinter的应用程序,试图管理游戏的设置。用户可能安装了此游戏的多个版本,在这种情况下,我需要询问用户他们想要在启动时管理哪个安装。那部分单独运作得很好;构建主窗口后弹出选择对话框,并以模态方式运行。

然而,由于游戏版本之间存在差异,如果我能够针对这些情况稍微调整界面将会很有用。但是,这意味着在我知道正在进行哪种安装之前,我无法真正构建主窗口,因此在用户做出选择之前它将是空白的。

我希望在显示此对话框时隐藏根窗口,但在根窗口上调用withdraw只会导致模式对话框也不显示 - Python最终会挂起而没有CPU使用情况,我无法弄清楚如何解决这个问题,而不必诉诸非模态窗口(以及一个我想避免的显着不同的控制流程)。

展示问题的示例代码和通用代码结构(Python 2.7):

from Tkinter import *
from ttk import *

class TkGui(object):
    def __init__(self):
        self.root = root = Tk()
        self.root.withdraw()
        selector = FolderSelection(self.root, ('foo', 'bar'))
        self.root.deiconify()
        print(selector.result)

class ChildWindow(object): #Base class
    def __init__(self, parent, title):
        top = self.top = Toplevel(parent)
        self.parent = parent
        top.title(title)
        f = Frame(top)
        self.create_controls(f)
        f.pack(fill=BOTH, expand=Y)

    def create_controls(self, container):
        pass

    def make_modal(self, on_cancel):
        self.top.transient(self.parent)
        self.top.wait_visibility() # Python will hang here...
        self.top.grab_set()
        self.top.focus_set()
        self.top.protocol("WM_DELETE_WINDOW", on_cancel)
        self.top.wait_window(self.top) # ...or here, if wait_visibility is removed

class FolderSelection(ChildWindow):
    def __init__(self, parent, folders):
        self.parent = parent
        self.listvar = Variable(parent)
        self.folderlist = None
        super(FolderSelection, self).__init__(parent, 'Select folder')
        self.result = ''
        self.listvar.set(folders)
        self.make_modal(self.cancel)

    def create_controls(self, container):
        f = Frame(container)
        Label(
            f, text='Please select the folder '
            'you would like to use.').grid(column=0, row=0)
        self.folderlist = Listbox(
            f, listvariable=self.listvar, activestyle='dotbox')
        self.folderlist.grid(column=0, row=1, sticky="nsew")
        Button(
            f, text='OK', command=self.ok
            ).grid(column=0, row=2, sticky="s")
        self.folderlist.bind("<Double-1>", lambda e: self.ok())
        f.pack(fill=BOTH, expand=Y)

    def ok(self):
        if len(self.folderlist.curselection()) != 0:
            self.result = self.folderlist.get(self.folderlist.curselection()[0])
            self.top.protocol('WM_DELETE_WINDOW', None)
            self.top.destroy()

    def cancel(self):
        self.top.destroy()

TkGui()

1 个答案:

答案 0 :(得分:2)

在您的情况下似乎没有差异,模态窗口与否。实际上,您只需要使用wait_window()方法。根据{{​​3}}:

  

在许多情况下,处理对话框更为实际   同步时尚;创建对话框,显示它,等待用户   关闭对话框,然后继续执行您的应用程序。   wait_window方法正是我们所需要的;它进入了一个地方   事件循环,并且在销毁给定窗口之前不会返回   (通过destroy方法,或通过窗口管理器显式)。

请考虑使用非模态窗口的示例:

from Tkinter import *

root = Tk()

def go():
   wdw = Toplevel()
   wdw.geometry('+400+400')
   e = Entry(wdw)
   e.pack()
   e.focus_set()
   #wdw.transient(root)
   #wdw.grab_set()
   root.wait_window(wdw)
   print 'done!'

Button(root, text='Go', command=go).pack()
Button(root, text='Quit', command=root.destroy).pack()

root.mainloop()

单击Go按钮时,将出现非模态对话框,但代码将停止执​​行,只有在关闭对话框窗口后才会显示字符串done!

如果这是您想要的行为,那么这是您修改后的表单中的示例(我在TkGui和__init__方法中修改了make_modal,还添加了mainloop()):

from Tkinter import *
from ttk import *

class TkGui(object):
    def __init__(self):
        self.root = root = Tk()
        self.root.withdraw()
        selector = FolderSelection(self.root, ('foo', 'bar'))
        self.root.deiconify()
        print(selector.result)

class ChildWindow(object): #Base class
    def __init__(self, parent, title):
        top = self.top = Toplevel(parent)
        self.parent = parent
        top.title(title)
        f = Frame(top)
        self.create_controls(f)
        f.pack(fill=BOTH, expand=Y)

    def create_controls(self, container):
        pass

    def make_modal(self, on_cancel):
        #self.top.transient(self.parent)
        #self.top.wait_visibility() # Python will hang here...
        #self.top.grab_set()
        self.top.focus_set()
        self.top.protocol("WM_DELETE_WINDOW", on_cancel)
        self.top.wait_window(self.top) # ...or here, if wait_visibility is removed

class FolderSelection(ChildWindow):
    def __init__(self, parent, folders):
        self.parent = parent
        self.listvar = Variable(parent)
        self.folderlist = None
        super(FolderSelection, self).__init__(parent, 'Select folder')
        self.result = ''
        self.listvar.set(folders)
        self.make_modal(self.cancel)

    def create_controls(self, container):
        f = Frame(container)
        Label(
            f, text='Please select the folder '
            'you would like to use.').grid(column=0, row=0)
        self.folderlist = Listbox(
            f, listvariable=self.listvar, activestyle='dotbox')
        self.folderlist.grid(column=0, row=1, sticky="nsew")
        Button(
            f, text='OK', command=self.ok
            ).grid(column=0, row=2, sticky="s")
        self.folderlist.bind("<Double-1>", lambda e: self.ok())
        f.pack(fill=BOTH, expand=Y)

    def ok(self):
        if len(self.folderlist.curselection()) != 0:
            self.result = self.folderlist.get(self.folderlist.curselection()[0])
            self.top.protocol('WM_DELETE_WINDOW', None)
            self.top.destroy()

    def cancel(self):
        self.top.destroy()

TkGui()
mainloop()

代码在selector = FolderSelection(self.root, ('foo', 'bar'))行停止,然后在关闭对话框后继续。