Python消息框显示而不停止脚本

2024-09-28 19:28:47 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

这是包含循环的代码部分:

 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()

Tags: 脚本消息目标tkinter数字errorredctypes
1条回答
网友
1楼 · 发布于 2024-09-28 19:28:47

大多数情况下(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()

相关问题 更多 >