Neo630
Neo630

Reputation: 191

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. 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.

This is gui.py where the method exists.

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

import gui

gui.app.display_message("this is from client.py")

Upvotes: 0

Views: 145

Answers (1)

jizhihaoSAMA
jizhihaoSAMA

Reputation: 12672

When you use 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.


Change the code in 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:

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()):

root = Tk()
app = App(root)
app.display_message("This is from gui.py")

And the code in client.py:

import gui
import tkinter as tk

test.app.display_message("this is from client.py")
test.root.mainloop()

This will display two messages in the gui. enter image description here

Upvotes: 1

Related Questions