【问题标题】:python dialogbox in thread线程中的python对话框
【发布时间】:2022-10-25 18:37:04
【问题描述】:

我写了一个简单的测试程序,它应该不断地在对话框中询问用户浮动而不阻塞主程序,为了简单起见,在测试中我删除了它:

from time import sleep
from PyQt5.QtWidgets import QApplication, QWidget, QInputDialog, QLineEdit
from PyQt5.QtGui import QIcon
from datetime import datetime, timedelta
import ctypes  # An included library with Python install.
import threading
import tkinter as tk
from tkinter import simpledialog


from queue import Queue # Python 3.x


def ask(x):
    ROOT = tk.Tk()

    ROOT.withdraw()
    # the input dialog
    P = simpledialog.askstring(title="Ref",
                                      prompt="Enter float:")
    if P=="999":
        #end
        return "end"
    else:
        return float(P)

UI=0
threads_running = []
end=0
que = Queue() 
while(end==0):
    if UI==0:
        UI=1
        th = threading.Thread(target=lambda q, arg1: q.put(ask(arg1)), args=(que, "x"))
        th.setDaemon(True)
        th.start()
        threads_running.append(th)
        
    for thread in threads_running:
        thread.join(timeout=0)  # non-blocking because timeout=0
        if thread.is_alive():
            # the join has timeout, so the thread is still running
            pass
        else:
            threads_running.remove(thread)
            print(threads_running)
            thread = threading.Thread(target=lambda q, arg1: q.put(ask(arg1)), args=(que, "x"))
            thread.setDaemon(True)
            thread.start()
            threads_running.append(thread)
        while not que.empty():
            result = que.get()
            print(result)
            if result=="end":
                end=1
        sleep(1)

它一直有效,直到该函数第二次运行,然后出现以下错误:

Exception in thread Thread-17:
Traceback (most recent call last):
  File "D:\Program Files\Python\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "D:\Program Files\Python\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "C:/Users/kevin/Desktop/PRC/test_dialog.py", line 47, in <lambda>
    thread = threading.Thread(target=lambda q, arg1: q.put(ask(arg1)), args=(que, "x"))
  File "C:/Users/kevin/Desktop/PRC/test_dialog.py", line 19, in ask
    P = simpledialog.askstring(title="Ref",
  File "D:\Program Files\Python\lib\tkinter\simpledialog.py", line 399, in askstring
    d = _QueryString(title, prompt, **kw)
  File "D:\Program Files\Python\lib\tkinter\simpledialog.py", line 376, in __init__
    _QueryDialog.__init__(self, *args, **kw)
  File "D:\Program Files\Python\lib\tkinter\simpledialog.py", line 271, in __init__
    Dialog.__init__(self, parent, title)
  File "D:\Program Files\Python\lib\tkinter\simpledialog.py", line 131, in __init__
    Toplevel.__init__(self, parent)
  File "D:\Program Files\Python\lib\tkinter\__init__.py", line 2616, in __init__
    BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra)
  File "D:\Program Files\Python\lib\tkinter\__init__.py", line 2567, in __init__
    self.tk.call(
RuntimeError: main thread is not in main loop

所以问题似乎出在 TKinter 上,但我不知道为什么它第一次起作用,但在调用第二个对话框时却不起作用。 有谁知道如何解决这个问题?谢谢

【问题讨论】:

    标签: python multithreading dialog


    【解决方案1】:

    这有帮助吗?在第 37-48 行,我删除了,所以不会再有错误:RuntimeError: main thread is not in main loop

    while(end==0):
        if UI==0:
            UI=1
            th = threading.Thread(target=lambda q, arg1: q.put(ask(arg1)), args=(que, "x"))
            th.setDaemon(True)
            th.start()
            threads_running.append(th)
            
        while not que.empty():
            result = que.get()
            print(result)
            if result=="end":
                end=1
        sleep(1)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多