How to disable window controls when a modal dialog box is active in TkInter?

一个人想着一个人 提交于 2019-12-11 10:46:50

问题


I am writing a Python application with TkInter. At some point the application (root) displays a dialog box (dlg, which is a Toplevel). In order to make the dialog modal I use the following code:

dlg.focus_set()
dlg.grab_set()
dlg.transient(root)
root.wait_window(dlg)

This indeed cancels "custom" events outside the dialog box (like the widgets in the main application window), but it does NOT cancel the window manager events, so that for example clicking on the main application window has it regain focus and it can be moved, resized - and even closed! - while the "modal" dialog is still open.

How can I make my dialog truly modal, so that window manager events for the main application window are also suspended while the dialog box is active?

I am using Python 3.4.3 on Ubuntu 15.04.


回答1:


you can use root.grab_set_global() as in this exemple:

import Tkinter
class Application(Tkinter.Frame):
    def mygrab(self):
        print "grab is ok"
        root.grab_set_global()

    def createWidgets(self):
        self.QUIT = Tkinter.Button(self)
        self.QUIT["text"] = "QUIT"
        self.QUIT["command"] =  self.quit
        self.QUIT.pack({"side": "left"})
        self.grab = Tkinter.Button(self)
        self.grab["text"] = "Grab",
        self.grab["command"] = self.mygrab
        self.grab.pack({"side": "left"})        

    def __init__(self, master=None):
        Tkinter.Frame.__init__(self, master)
        self.pack()
        self.createWidgets()

root = Tkinter.Tk()
app = Application(master=root)
app.mainloop()
root.destroy()`



回答2:


Try this way:

dlg.focus_set()
dlg.grab_set()
dlg.transient(root)
dlg.wait_window(dlg)


来源:https://stackoverflow.com/questions/31892015/how-to-disable-window-controls-when-a-modal-dialog-box-is-active-in-tkinter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!