[英]tkinter user non closeable window
我正在尝试编写一个程序,其中我删除了主窗口关闭选项并为用户提供了一个退出按钮来关闭程序。
按下后,我需要在后台进行一些耗时的处理,我不希望用户在意外发生时关闭程序。 有没有办法从显示的消息框中删除所有按钮?
import tkinter as tk
from win32api import GetSystemMetrics
from tkinter import messagebox
def on_closing():
pass
def exit():
messagebox.showinfo("Wait", "Please wait for background process to complete")
root.destroy()
root = tk.Tk()
root.resizable(width=False, height=False)
root.protocol("WM_DELETE_WINDOW", on_closing)
width = GetSystemMetrics(0)
height = GetSystemMetrics(1)
root.geometry('{}x{}'.format(width,height))
exitButton = tk.Button(root,text="Exit",width=15,command=exit)
exitButton.grid(row=0,column=1,padx=6,pady=6)
root.overrideredirect(True)
root.mainloop()
在后台:用户的机器上生成了一些文件,我想使用 python 库将它们存档。 这些文件可能会在某个时候达到 1GB,所以我认为如果运行它的笔记本电脑的计算能力非常低,它会花费更多的时间。 这就是我的基地的情况,因此我希望他们只是等到弹出窗口关闭。 这我可以在用户手册中定义。
我不确定你想做什么工作,但对于这个例子,我正在做一个打印一些东西然后睡觉然后打印它的工作。 所以这大约需要 20 秒。 在这 20 秒内,您将无法退出 GUI。
import tkinter as tk
from win32api import GetSystemMetrics
from tkinter import messagebox
import time
import threading
def on_closing():
if started == False: #if work is not going on, then quit
root.destroy()
else: # else show the message.
messagebox.showinfo("Wait", "Please wait for background process to complete")
def work():
global started
started = True #mentioning that the work started
print('Hey')
time.sleep(5)
print('There')
time.sleep(5)
print('Whats Up')
time.sleep(5)
print('Cool?')
time.sleep(5)
started = False #mentioning that the work stopped
started = False #initially work is not started
root = tk.Tk()
root.resizable(width=False, height=False)
root.protocol("WM_DELETE_WINDOW", on_closing)
width = GetSystemMetrics(0)
height = GetSystemMetrics(1)
root.geometry('{}x{}'.format(width,height))
exitButton = tk.Button(root,text="Exit",width=15,command=on_closing)
exitButton.grid(row=0,column=1,padx=6,pady=6)
Button = tk.Button(root,text="Work",width=15,command=threading.Thread(target=work).start)
Button.grid(row=1,column=1,padx=6,pady=6)
# root.overrideredirect(True)
root.mainloop()
在这里, started
就像一面旗帜。 您必须在开始工作之前将其设置为True
,并在工作结束后将其设置为False
。
您可以忽略我创建了一个新按钮并使用了threading
的事实,这只是向您模拟一个完成工作的示例。 线程有助于 GUI 不冻结。 虽然我不确定这是否适用于root.overrideredirect(True)
,但我认为你可以摆脱它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.