[英]Calling a method from a class from another file giving weird error in tkinter
I am trying to call the method display_message
from the client.py
file and for some reason it is returning a weird error.我正在尝试从
client.py
文件中调用方法display_message
并且由于某种原因它返回一个奇怪的错误。 I run the same method from within the same file and it works perfectly fine.我从同一个文件中运行相同的方法,它工作得很好。
Error
错误
Traceback (most recent call last):
File "client.py", line 2, in <module>
app.display_message("hello")
File "/Users/Neo630/Desktop/PyChat/gui.py", line 34, in display_message
self.message_area.insert(INSERT, message + "\n")
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 3269, in insert
self.tk.call((self._w, 'insert', index, chars) + args)
_tkinter.TclError: invalid command name ".!frame2.!text"
The method displays text in a text widget in tkinter.该方法在 tkinter 的文本小部件中显示文本。
This is
gui.py
where the method exists.这是该方法所在的
gui.py
from tkinter import *
class App:
def __init__(self, master):
self.master = master
master.title("PyChat")
master.geometry("800x500")
master.configure(bg="grey")
master.grid_columnconfigure((0,1,2), uniform="uniform", weight=1)
master.grid_rowconfigure(0, weight=1)
self.friends_space = Frame(master, bg="red")
self.friends_space.grid(row=0, column=0, sticky=NSEW)
self.chat_space = Frame(master, bg="blue")
self.chat_space.grid(row=0, column=1, columnspan=3, sticky=NSEW)
self.message_area = Text(self.chat_space, width=1, height=1)
self.message_area.pack(fill=BOTH, expand=True, side=TOP, padx=10, pady=10)
self.message_input = Entry(self.chat_space)
self.message_input.pack(fill=X, side=BOTTOM, padx=10, pady=(0,10))
self.message_input.bind('<Return>', self.get_message_input)
def get_message_input(self, event):
global message
message = self.message_input.get()
self.message_input.delete(0, END)
def display_message(self, message):
self.message_area.insert(INSERT, message + "\n")
self.message_area.see(END)
root = Tk()
app = App(root)
app.display_message("This is from gui.py")
root.mainloop()
This is
client.py
where I am attempting to call the method这是
client.py
我试图在其中调用该方法
import gui
gui.app.display_message("this is from client.py")
When you use import gui
.当您使用
import gui
。
The code in the end will run:代码最终会运行:
root = Tk()
app = App(root)
app.display_message("This is from gui.py")
root.mainloop()
But after you close the window, the mainloop()
end, then it will run gui.app.display_message("this is from client.py")
.But at this time, this window(the frame, too) has been destroyed.That's why it showed the error.但是当你关闭窗口后,
mainloop()
结束,然后它会运行gui.app.display_message("this is from client.py")
。但此时,这个窗口(框架)也被销毁了。这就是它显示错误的原因。
Change the code in gui.py:更改 gui.py 中的代码:
if __name__ == '__main__':
root = Tk()
app = App(root)
app.display_message("This is from gui.py")
root.mainloop()
And the code in client.py
:和
client.py
的代码:
import gui
import tkinter as tk
root = tk.Tk()
app = test.App(root)
app.display_message("this is from client.py")
root.mainloop()
The change below maybe isn't what you want, you could also try:下面的更改可能不是您想要的,您也可以尝试:
The code in gui.py
(Don't use mainloop()
): gui.py
的代码(不要使用mainloop()
):
root = Tk()
app = App(root)
app.display_message("This is from gui.py")
And the code in client.py
:和
client.py
的代码:
import gui
import tkinter as tk
test.app.display_message("this is from client.py")
test.root.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.