Python Tkinter。仅显示一份 window

Python Tkinter. Show only one copy of window

我有一个问题。我有在 Tkinter 上写的程序。

import Tkinter as tk
import ttk


def about_window():
    print(root.child_window)

    if not root.child_window:
        top2 = tk.Toplevel(root)
        top2.title("About")
        top2.resizable(0,0)

        explanation = "This program is my test program"

        ttk.Label(top2,justify=tk.LEFT,text=explanation).pack(padx=5,pady=2)
        ttk.Button(top2,text='OK',width=10,command=top2.destroy).pack(pady=8)


root = tk.Tk()
root.resizable(0,0)

root.child_window = None
#print(root.child_window)

root.style = ttk.Style()
# ('clam', 'alt', 'default', 'classic')
root.style.theme_use("clam")

menu = tk.Menu(root)
root.config(menu=menu)

fm = tk.Menu(menu, tearoff=0)
menu.add_cascade(label="Settings",menu=fm)
fm.add_command(label="Preferances")

hm = tk.Menu(menu, tearoff=0)
menu.add_cascade(label="Help",menu=hm)
hm.add_command(label="About", command=about_window)
hm.add_command(label="Exit",command=root.quit)
#

tk.mainloop()

所以我可以点击 "About" 标签然后会看到 window:

但是在 Tkinter 中是否可以禁用相同 window 的任何下一次启动?

我已经试过了 但 w/o 成功了。

制作 about window 模态的最简单方法是将以下行添加到 about_window() 函数中:

top2.focus_set()                                                        
top2.grab_set()    

第一行将焦点设置到您的关于 window,

第二行禁止任何其他window接受事件。

一种方法是使 child window 暂时成为根目录,这样在 child 关闭之前您无法与根目录交互(您不需要 root.child_window 这里):

import Tkinter as tk
import ttk

def about_window(): 
    top2 = tk.Toplevel(root)
    top2.title("About")
    top2.resizable(0,0)

    explanation = "This program is my test program"

    ttk.Label(top2,justify=tk.LEFT,text=explanation).pack(padx=5,pady=2)
    ttk.Button(top2,text='OK',width=10,command=top2.destroy).pack(pady=8)

    top2.transient(root)
    top2.grab_set()
    root.wait_window(top2)

root = tk.Tk()
root.resizable(0,0)

root.style = ttk.Style()
# ('clam', 'alt', 'default', 'classic')
root.style.theme_use("clam")

menu = tk.Menu(root)
root.config(menu=menu)

fm = tk.Menu(menu, tearoff=0)
menu.add_cascade(label="Settings",menu=fm)
fm.add_command(label="Preferances")

hm = tk.Menu(menu, tearoff=0)
menu.add_cascade(label="Help",menu=hm)
hm.add_command(label="About", command=about_window)
hm.add_command(label="Exit",command=root.quit)
#

tk.mainloop()

通过使用 Class 来表示您的程序,另一种方法变得容易得多。它本质上归结为需要一个全局(或 class 实例的范围)变量。在这种情况下,我们使用 self.top2 可以在方法 about_window().

的内部和外部访问

此方法将允许在子 window 打开时与根 window 进行完全交互。

import Tkinter as tk
import ttk


class MyProgram():

    def __init__(self):

        self.top2 = None
        self.root = root = tk.Tk()
        root.resizable(0,0)

        root.style = ttk.Style()
        # ('clam', 'alt', 'default', 'classic')
        root.style.theme_use("clam")

        menu = tk.Menu(root)
        root.config(menu=menu)

        fm = tk.Menu(menu, tearoff=0)
        menu.add_cascade(label="Settings",menu=fm)
        fm.add_command(label="Preferances")

        hm = tk.Menu(menu, tearoff=0)
        menu.add_cascade(label="Help",menu=hm)
        hm.add_command(label="About", command=self.about_window)
        hm.add_command(label="Exit",command=root.quit)

    def about_window(self):
        try:
            self.top2.focus_set()
            return
        except Exception:
            pass

        self.top2 = top2 = tk.Toplevel(self.root)
        top2.title("About")
        top2.resizable(0,0)

        explanation = "This program is my test program"

        ttk.Label(top2,justify=tk.LEFT,text=explanation).pack(padx=5,pady=2)
        ttk.Button(top2,text='OK',width=10,command=top2.destroy).pack(pady=8)


MyProgram()
tk.mainloop()

状态(新状态=None)

Returns the window's current state, one of:

    'normal': Displayed normally.

    'iconic': Iconified with the .iconify() method.

    'withdrawn': Hidden; see the .withdraw() method below.