【问题标题】:Python message box showing without stopping scriptPython 消息框在不停止脚本的情况下显示
【发布时间】:2013-04-18 12:33:24
【问题描述】:

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

我一直在论坛里找资料,但我没有找到有这个问题的人,所以我希望有人能帮助我。

这是带有循环的代码部分:

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

【问题讨论】:

    标签: python loops messagebox


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2021-05-04
      • 2021-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多