在用python3.10的tkinter编程时发现这样一个错误:
Exception in thread Thread-7:
Traceback (most recent call last):
File "C:\Users\AppData\Local\Programs\Python\Python310\lib\threading.py", line 1009, in _bootstrap_inner
self.run()
File "C:\Users\AppData\Local\Programs\Python\Python310\lib\threading.py", line 1358, in run
self.function(*self.args, **self.kwargs)
File "D:\Python\English_Parser\main.py", line 42, in timing1
self.ago1 = self.entry.get()
File "C:\Users\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 3072, in get
return self.tk.call(self._w, 'get')
RuntimeError: main thread is not in main loop
使用的时候并没有问题,只是在按关闭页面之后会报错
源码:
from tkinter import *
import parser
from time import sleep
import threading
class Application(Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.label1 = Label(text='请输入:') # 设置初始界面
self.label1.grid(column=0, row=0)
self.entry = Entry()
self.entry.grid(column=1, row=0)
self.translator()
def translator(self): # 翻译
def translate(word):
tree = parser.get_data(word) # 获取网页解析
phonetic_symbol = StringVar() # 音标
phonetic_symbols = parser.get_phonetic_symbol(tree)
phonetic_symbol.set('Br : ' + phonetic_symbols['Br'] + ' Am : ' + phonetic_symbols['Am'])
Entry(textvariable=phonetic_symbol).grid(column=0, row=1, columnspan=2)
meanings = StringVar() # 释义
meanings.set(parser.get_meanings(tree))
text = Text(width=50, height=10)
text.grid(column=2, row=0, columnspan=2, rowspan=2)
text.delete(1.0, END)
text.insert(1.0, meanings.get())
def timing1():
global timer
self.ago1 = self.entry.get()
if self.ago1 != self.ago2:
translate(self.entry.get())
timer = threading.Timer(1, timing2)
timer.start()
def timing2():
global timer
self.ago2 = self.entry.get()
if self.ago1 != self.ago2:
translate(self.entry.get())
timer = threading.Timer(1, timing1)
timer.start()
self.ago1 = self.ago2 = str()
timer = threading.Timer(1, timing1) # 计时器
timer.start()
if __name__ == '__main__':
root = Tk()
root.title('EnglishParser')
root.geometry('800x400+400+200')
app = Application(master=root)
root.mainloop()
别的tkinter都没问题
就比如:
from tkinter import *
root = Tk()
root.mainloop()
正常关闭,没有报错,所以到底怎么回事呢