簡體   English   中英

顯示不停止腳本的Python消息框

[英]Python message box showing without stopping script

我有一個腳本,可以無限循環地在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()

在大多數情況下,消息框(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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM