【发布时间】:2021-07-10 16:53:46
【问题描述】:
我想在调用mainloop() 后更改框架中显示的文本。我创建了loginfo 函数来在我的字符串中附加文本,但没有任何反应。 GUI 启动并显示最初包含在其中的文本(“hi”),我没有看到通过 loginfo 函数添加的文本(“hello”),退出 GUI 后出现以下错误。
Traceback (most recent call last):
File "1.py", line 5, in <module>
monitor.loginfo()
File "/home/shreyas/Desktop/test/main.py", line 45, in loginfo
self.text.configure(state='normal')
File "/usr/lib/python3.8/tkinter/__init__.py", line 1637, in configure
return self._configure('configure', cnf, kw)
File "/usr/lib/python3.8/tkinter/__init__.py", line 1627, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: invalid command name ".!frame.!text"
我的任务是创建一个函数,我可以随时使用我想要插入的文本调用该函数。 当我收到要显示的文本时,将在 mainloop 运行后调用该函数。
这是我创建的 2 个文件:
main.py
import tkinter
from tkinter import *
class Monitor:
def __init__(self):
self.root = Tk()
self.root.title('Monitor')
self.root.geometry("800x400")
self.root.grid_columnconfigure((0,1), weight=1)
self.root.grid_rowconfigure(0, weight=1)
"""-----------------------------------------------"""
self.console = Frame(self.root,borderwidth=1)
self.console.grid(row = 0, column = 0, sticky = W+E+N+S)
self.console.grid_columnconfigure(0, weight=1)
self.console.grid_rowconfigure(2, weight=1)
self.lbl_c = Label(self.console, text="console",bg='white')
self.lbl_c.grid(row = 1, column = 0, sticky = W+E+N+S)
self.text = tkinter.Text(self.console)
self.text.grid(row = 2, column = 0,rowspan = 3, columnspan = 1, sticky = N+S+E+W)
self.text.insert(tkinter.END,"hi")
self.text.configure(state='disabled')
"""------------------------------------------------"""
self.fm = Frame(self.root,borderwidth=1)
self.fm.grid(row = 0, column = 1, sticky = W+E+N+S)
self.fm.grid_columnconfigure(0, weight=1)
self.fm.grid_rowconfigure(2, weight=1)
self.lbl_fm = Label(self.fm, text="frequency_monitor",bg='white')
self.lbl_fm.grid(row = 1, column = 0, sticky = W+E+N+S)
self.text1 = tkinter.Text(self.fm)
self.text1.grid(row = 2, column = 0,rowspan = 1, columnspan = 1, sticky = N+S+E+W)
self.text1.insert(tkinter.END,"<---------- Frequency Monitor ---------->\n\n"+"Camera100\n"+"Frequency: 9.6 CPU Time: 3.0ms\n"+("----------------------------------------")+"Screen100\n"+"Frequency: 29.8 CPU Time: 6.0ms\n"+("----------------------------------------"))
self.text1.configure(state='disabled')
def loginfo(self):
self.text.configure(state='normal')
self.text.insert(tkinter.END,"hello")
self.text.update()
self.text.configure(state='disabled')
1.py
import main as m
monitor = m.Monitor()
monitor.root.mainloop()
monitor.loginfo()
我使用 python 3.1 来运行我的代码。有人可以告诉我是什么导致了错误,我怎样才能达到预期的结果。
更新: 当我像这样使用 mainloop() 时
import main as m
monitor = m.Monitor()
monitor.root.mainloop()
monitor.root.update()
monitor.root.update_idletasks()
monitor.loginfo()
我得到了同样的错误,但是当我使用 while 时
import main as m
monitor = m.Monitor()
#monitor.root.mainloop()
#monitor.loginfo()
while True:
monitor.root.update()
monitor.root.update_idletasks()
monitor.loginfo()
它更新文本并不断更新它,因为我在 while 中调用了 loginfo 但如果我在 while 循环之外调用它,它不会更新。
【问题讨论】:
-
该错误表示您销毁了
Frame中的Text小部件,现在尝试访问它。 -
这是否意味着当我的 mainloop() 正在运行下面的函数时它不会被调用,当我关闭 GUI 时它会被破坏并且我的函数会尝试访问它?
-
是的,就是这样。
-
所以知道我该如何完成我的任务@CoolCloud
标签: python tkinter tkinter-text