尝试在不等待用户响应的情况下打开和关闭弹出窗口。
我试过:
root = Tk()
root.withdraw()
try:
root.after(TIME_TO_WAIT, root.destroy)
messagebox.showinfo("Output", "somthing")
except:
pass
或:
root = Tk()
root.withdraw()
try:
root.after(TIME_TO_WAIT, root.destroy)
Message(title="your title", message="somthing", master=root).show()
except:
pass
但使用 macOS 时会出现以下问题:
2023-02-28 15:12:08.197 Python[10165:332611] Warning: Expected min height of view: (<NSButton: 0x7fe48210d240>) to be less than or equal to 30 but got a height of 32.000000. This error will be logged once per view in violation.
2023-02-28 15:12:50.709 Python[10165:332611] Warning: Expected min height of view: (<NSButton: 0x7fe480190bf0>) to be less than or equal to 30 but got a height of 32.000000. This error will be logged once per view in violation.
objc[10165]: autorelease pool page 0x7fe47c277000 corrupted
magic 0x00000000 0x00000000 0x00000000 0x00000000
should be 0xa1a1a1a1 0x4f545541 0x454c4552 0x21455341
pthread 0x7ff85525f4c0
should be 0x7ff85525f4c0
我会做一些事情:
import tkinter as tk
import tkinter.messagebox as msgbox
def showMessage(message, timeout=5000):
root = tk.Tk()
root.withdraw()
root.after(timeout, root.destroy)
msgbox.showinfo('Info', message, master=root)
showMessage('Your Message Here')
这是一个潜在的解决方案,它创建一个
Toplevel
窗口,该窗口在 5 秒后自动关闭。
import tkinter as tk
def close():
dialog.destroy()
root.quit()
root = tk.Tk()
root.withdraw()
root.after(5000, close)
# create dialog window
dialog = tk.Toplevel(root)
dialog.geometry('200x100')
dialog.title('Hello')
message = tk.Label(dialog, text='Please Wait...')
message.pack(expand=True, fill='both')
# run app
if __name__ == '__main__':
root.mainloop()
但是,如果您不想为
tkinter.messagebox
烦恼,您可以跳过创建 Toplevel
窗口,只需使用主窗口作为您的消息对话框! :)
import tkinter as tk
root = tk.Tk()
root.geometry('200x100')
root.title('Hello')
root.after(5000, root.quit) # quit the app automatically after 5 sec
message = tk.Label(root, text='Please Wait...')
message.pack(expand=True, fill='both')
# run app
if __name__ == '__main__':
root.mainloop()