gpt4 book ai didi

python - tkMessageBox 在定时器程序中有额外的窗口

转载 作者:行者123 更新时间:2023-12-01 04:58:54 28 4
gpt4 key购买 nike

我正在用 Python 做一个计时器,但是当计时器停止时,我的代码最终显示两个对话框而不是一个。我该如何避免出现额外的窗口?

下面的timer.py代码:

#!/usr/bin/python
import time
start = time.time()
interval=1

import sys
import tkMessageBox

def showmessage():
tkMessageBox.showinfo("timer.py", "Timer Complete!")

if __name__ == "__main__":
if len(sys.argv) > 1:
ns = {'__builtins__': None}
seconds=int(eval(str(sys.argv[1]), ns))
if len(sys.argv) > 2:
if sys.argv[2]=="mins" or sys.argv[2]=="min":
seconds=seconds*60

interval = 1 if seconds < interval else interval
second=seconds - (time.time() - start) + 0.5
while second >= 0:
#In Python 3: print('string', end='\r')
if int(second/3600) > 0:
print "\rTime =%3d hrs %02d mins %02d sec" % \
(second/3600, (second % 3600)/60, second % 60),
else:
print "\rTime =%3d mins %02d sec" % \
(second/60, second % 60),

sys.stdout.flush()
time.sleep(interval)

if int(second % 60+0.5) == 0:
print #or sys.stdout.write("\n")
second=seconds - (time.time() - start) + 0.5

showmessage()
else:
print ("Usage: %s [seconds]" % str(sys.argv[0]))
print (" %s [minutes] mins" % str(sys.argv[0]))

最佳答案

发生这种情况是因为如果将 showmessage 更改为以下内容,消息框需要一个父窗口:

def showmessage():
root = tk.Tk()
root.withdraw()
tkMessageBox.showinfo("timer.py", "Timer Complete!")
root.destroy()

并添加:

import Tkinter as tk

到你的导入,那么你只会得到消息框

关于python - tkMessageBox 在定时器程序中有额外的窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26748959/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com