Python 3.x Python tinker非阻塞消息框多线程

Python 3.x Python tinker非阻塞消息框多线程,python-3.x,tkinter,Python 3.x,Tkinter,尝试在tinker中创建简单的非阻塞消息框,这样它将显示信息,并按照我喜欢的窗口消息框概念完成程序 我的代码如下 import threading from disp_message import disp_message msg =" This is a test message" msgtype = 1 t1 = threading.Thread(target=disp_message, args=(msg,msgtype,)) t1.start() t1.join() for i i

尝试在tinker中创建简单的非阻塞消息框,这样它将显示信息,并按照我喜欢的窗口消息框概念完成程序

我的代码如下

import threading
from disp_message import disp_message

msg =" This is a test message"
msgtype = 1

t1 = threading.Thread(target=disp_message, args=(msg,msgtype,))
t1.start()
t1.join()

for i in range(100000):
    print(i)
disp_message(msg,msgtype)
print("Done!")

disp_messagee function code is

from tkinter import *
from tkinter import messagebox

def disp_message(msg,msgtype):
    top = Tk()
    top.withdraw()
    if msgtype==1:
        messagebox.showwarning("Warning",msg)
    elif msgtype==2:
         messagebox.showinfo("information",msg)
    else:
        messagebox.showerror("Error",msg)
当我运行一个程序时,它会给我以下错误

 Traceback (most recent call last):
  File "toto.py", line 13, in <module>
    disp_message(msg,msgtype)
  File "c:\NSE\scripts\disp_message.py", line 8, in disp_message
    messagebox.showwarning("Warning",msg)
  File "C:\ProgramData\Anaconda\lib\tkinter\messagebox.py", line 87, in showwarning
    return _show(title, message, WARNING, OK, **options)
  File "C:\ProgramData\Anaconda\lib\tkinter\messagebox.py", line 72, in _show
    res = Message(**options).show()
  File "C:\ProgramData\Anaconda\lib\tkinter\commondialog.py", line 39, in show
    w = Frame(self.master)
  File "C:\ProgramData\Anaconda\lib\tkinter\__init__.py", line 2744, in __init__
    Widget.__init__(self, master, 'frame', cnf, {}, extra)
  File "C:\ProgramData\Anaconda\lib\tkinter\__init__.py", line 2299, in __init__
    (widgetName, self._w) + extra + self._options(cnf))
RuntimeError: main thread is not in main loop
回溯(最近一次呼叫最后一次):
文件“toto.py”,第13行,在
显示消息(消息,消息类型)
文件“c:\NSE\scripts\disp\u message.py”,disp\u message中的第8行
messagebox.showwarning(“警告”,msg)
文件“C:\ProgramData\Anaconda\lib\tkinter\messagebox.py”,第87行,在showwarning中
返回显示(标题、消息、警告、确定、**选项)
文件“C:\ProgramData\Anaconda\lib\tkinter\messagebox.py”,第72行,显示
res=消息(**选项).show()
文件“C:\ProgramData\Anaconda\lib\tkinter\commondialog.py”,第39行,在show中
w=帧(self.master)
文件“C:\ProgramData\Anaconda\lib\tkinter\\ uuuuu init\uuuuu.py”,第2744行,在\uuu init中__
Widget.\uuuu init\uuuuuu(self、master、'frame',cnf、{}、extra)
文件“C:\ProgramData\Anaconda\lib\tkinter\\uuuuu init\uuuuu.py”,第2299行,在\uuuu init中__
(widgetName,self._w)+额外+自选项(cnf))
运行时错误:主线程不在主循环中
其次,它不会并行显示tkinter消息框


你能帮我吗?其次,我不确定t1.join()是否是必需的,因为我的目标是只显示消息并完全执行程序。

from disp_message import disp_message
中的
disp_message是什么(target=disp_msage,args=(msg,msgtype,)
,当然会在错误中重复!最好是提出新问题,而不是通过更正指出给您的错误来修改原始问题。
tkinter
不支持线程;所有
tkinter
元素必须在主线程中。如果仍然有问题,请提出新问题。OK@Reblochon M我要问一个新问题