[英]Python Tkinter. Show only one copy of window
I have one problem. 我有一个问题。 I have program which has been wrote on Tkinter.
我有在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()
So I can click on "About" label and will see window: 所以我可以点击“关于”标签,然后会看到窗口:
But is it possible in Tkinter to disable any next launch of the same window? 但是在Tkinter中是否可以禁用同一窗口的任何下一次启动?
I`ve tried this https://stackoverflow.com/a/24886806/2971192 but w/o success. 我试过这个https://stackoverflow.com/a/24886806/2971192但没有成功。
One way is to make the child window transient to the root, so that you cannot interact with root until the child has been closed ( you don't need root.child_window here): 一种方法是让子窗口瞬态到根,这样在子关闭之前你不能与root交互(你不需要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()
The easiest way to make your about window modal is afaik adding the follwing lines to your about_window()
function: 制作关于窗口模态的最简单方法是afaik
about_window()
添加到about_window()
函数中:
top2.focus_set()
top2.grab_set()
the first line sets the focus to your about window, 第一行将焦点设置到您的窗口,
the second line prohibits any other window to accept events. 第二行禁止任何其他窗口接受事件。
The other method is made a lot easier by using a Class to represent your program. 通过使用Class来表示您的程序,另一种方法变得更加容易。 It essentially boils down to the need for a global (or scope of the class instance) variable.
它基本上归结为对全局(或类实例的范围)变量的需求。 In this case we use
self.top2
which can be accessed inside and outside the method about_window()
. 在这种情况下,我们使用
self.top2
,可以在方法about_window()
内部和外部访问。
This method will allow for full interaction with the root window while the child window is open. 当子窗口打开时,此方法将允许与根窗口完全交互。
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()
state(newstate=None) 状态(newstate =无)
Returns the window's current state, one of:
'normal': Displayed normally.
'iconic': Iconified with the .iconify() method.
'withdrawn': Hidden; see the .withdraw() method below.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.