如何在脚本运行时显示消息

0 投票
1 回答
1568 浏览
提问于 2025-04-18 01:10

我写了一个Python脚本,这个脚本可以运行长达一分钟。在脚本运行的时候,我想给用户一个提示框,告诉他们脚本正在运行。

一种可行的方法是在脚本开始时显示一个消息,然后在脚本完成时关闭这个消息。

现在我使用tkMessageBox来提供一些信息。我也看过ctypes.windll.user32.MessageBoxA,但没有找到合适的解决办法。

1 个回答

0

我很久以前就尝试过这样做。我知道这不是个好办法,可能Python也不太喜欢我这么做,但对我来说还是能用。

你可能还是会看到错误信息,但它确实能工作。也许其他人能帮我改进一下,找到更好的方法。下面是代码:

import Tkinter
import tkMessageBox
import thread

top = Tkinter.Tk()
def hello():
    tkMessageBox.showinfo("Say Hello", "Hello World")

B1 = Tkinter.Button(top, text = "Say Hello", command = hello)
B1.pack()

thread_to_stop = thread
try:
    thread_to_stop.start_new_thread(top.mainloop,(0,))
except:
    pass

print "Python doesn't like this very much"
raw_input("You choose when to close it!")
# Do more stuff here....
top.destroy()

撰写回答