Python消息框显示没有停止脚本

时间:2013-04-18 12:33:25

标签: python loops messagebox

我有一个脚本可以在无限循环中随机创建5到55之间的数字。我的目标是在创建数字55时显示一个消息框。我已经尝试了easygui,tkinter,ctypes,...我已经能够创建消息框,但是当此框出现时,循环停止并且不会继续,直到用户单击“确定”按钮。有没有办法在不停止脚本的情况下显示消息框?

我一直在论坛上寻找信息,但我没有找到有这个问题的人,所以我希望有人可以帮助我。

这是循环代码的一部分:

 def contador():
  for x in range(1):
    aleatorio = random.randint(1,11)*5
    if aleatorio ==55:
        estado = "Red"
        ctypes.windll.user32.MessageBoxW(0, u"Error", u"Error", 0)
    elif aleatorio >=30:
        estado = "Red"

    else:
        estado = "Green"

    global t 
 t = threading.Timer(15.0, contador)
 t.start()

t = threading.Timer(15.0, contador)
t.start()

1 个答案:

答案 0 :(得分:0)

在大多数情况下(tkinter,gtk,winapi)的消息框都是模态窗口。您可以创建自己的对话框,也可以使用线程。

import random
from Tkinter import Tk

def show_error(text):
    top = Toplevel(root)
    Label(top, text=text).pack()
    Button(top, text="OK", command=top.destroy).pack(pady=5)

def contador():
    for x in range(100):
        aleatorio = random.randint(1,11)*5
        if aleatorio == 55:
            estado = "Red"
            show_error('Some error occurred: %s' % x)
        elif aleatorio >=30:
            estado = "Red"
        else:
            estado = "Green"

contador()