简体   繁体   English

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

[英]Python message box showing without stopping script

I have an script that create numbers randomly, between 5 and 55, in a infinite loop. 我有一个脚本,可以无限循环地在5到55之间随机创建数字。 My goal is to show a message box when the number 55 is created. 我的目标是在创建数字55时显示一个消息框。 I have try easygui, tkinter, ctypes,... and I have been able to create the message box, but when this box appears the loop stops and it doesn't continue untill the user clicks de OK button. 我尝试过easygui,tkinter,ctypes等,并且已经能够创建消息框,但是当此框出现时,循环停止,并且直到用户单击“确定”按钮后,该循环才继续。 Is there a way to show a message box without stopping the script? 有没有一种方法可以显示消息框而不停止脚本?

I have been looking for information in forums but I haven't found someone with this problem, so I hope someone can help me. 我一直在论坛中寻找信息,但没有找到有人遇到此问题,因此希望有人可以帮助我。

This is the part of the code with the loop: 这是循环代码的一部分:

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

messageboxes in most cases (tkinter, gtk, winapi) are modal windows. 在大多数情况下,消息框(tkinter,gtk,winapi)是模式窗口。 You can create your own dialog or you can use threads. 您可以创建自己的对话框,也可以使用线程。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM