如果“顶级”窗口当前处于打开状态,我试图跳过此功能。这是为了避免用户向按钮发送垃圾邮件并创建十亿个额外的通知窗口。那或如果“顶层”窗口可以在指定的时间后自动关闭,也将起作用。代码如下。非常感谢任何帮助,因为我是Python的新手。谢谢!
def pingClick():
os.system(
'-c -H ' + tkvar1.get() + ' -B ' + tkvar2.get() + ' -L ' + tkvar3.get() + ' -V ' + tkvar4.get() + ' -T log.log')
top = Toplevel()
top.title("Ping")
msg = Message(top, text=" - Notice - ")
msg.pack()
button = ttk.Button(top, text="Dismiss", command=top.destroy)
button.pack()
答案 0 :(得分:0)
创建全局变量top = None
。创建Toplevel()
时,请首先检查top
是否为None
。如果不是None
,则不要创建窗口。当您单击按钮销毁窗口时,您必须运行将其设置回top = None
的功能。当您使用按钮[X]
您可以使用top.after(3000, ping_destroy)
在3000毫秒(3秒)后关闭窗口
import tkinter as tk
# --- functions ---
def ping_destroy():
global top
if top is not None:
top.destroy()
top = None
def ping_click():
global top
if top is None:
#os.system('-c -H ' + tkvar1.get() + ' -B ' + tkvar2.get() + ' -L ' + tkvar3.get() + ' -V ' + tkvar4.get() + ' -T log.log')
top = tk.Toplevel()
top.title("Ping")
msg = tk.Message(top, text="Notice")
msg.pack()
button = tk.Button(top, text="Dismiss", command=ping_destroy)
button.pack()
top.wm_protocol('WM_DELETE_WINDOW', ping_destroy) # run `ping_destroy` when use button [X]
top.after(3000, ping_destroy) # close after 3000ms (3s)
# --- main ---
top = None
root = tk.Tk()
button = tk.Button(root, text='Ping', command=ping_click)
button.pack()
root.mainloop()