Insert text via pythons .insert() method to a tkinter Text() widget that resides in another file

Question:

I have 2 files.

app.py is the tkinter file that has everything tk related.

app_functions.py is just functions.

So when I run app.py and when I click on a tk button the command executes a function in the app_functions.py file but then in that very function it needs to .insert() text to a tk Text() widget in the app.py file. But I am getting errors.

Here is the error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:UsersPhil-AppDataLocalProgramsPythonPython310libtkinter__init__.py", line 1921, in __call__
    return self.func(*args)
  File "c:UsersPhil-python_maingsc script building appapp.py", line 30, in <lambda>
    button1 = Button(content_frame1, text="INIT_Function", command=lambda: app_functions.display_raw_gsc_code("INIT_FUNCTION_START", "INIT_FUNCTION_END"))
  File "c:UsersPhil-python_maingsc script building appapp_functions.py", line 45, in display_raw_gsc_code
    content_frame2_text_area.insert(tk.END, line)
NameError: name 'content_frame2_text_area' is not defined

When I import the app.py file within the app_functions.py file and then run the app.py file it loads up the gui and then once I click on the button it then opens up the tk gui again so that is no good.

So in short I am able to execute a function thats in another file from a tk button as I successfully managed to import the function.
But in that function it needs to .insert() text to a tk widget in another file, but this is not working out for me and all examples online include having the function in the same file as the tk button & tk Text() widget and sure it works, but i want to keep tk stuff and functions in separate files.

Basic concept of what im trying to accomplish:

  1. click button in app.py which executes a function called display_raw_gsc_code in app_functions.py
  2. display_raw_gsc_code function in app_functions.py does its job and then inserts text to a Text() widget in app.py
  3. Text() widget in app.py displays the received text.

BUTTON IN TK (app.py) FILE

button1 = Button(content_frame1, text="INIT_Function", command=lambda: app_functions.display_raw_gsc_code("INIT_FUNCTION_START", "INIT_FUNCTION_END"))

FUNCTION IN FUNCTIONS(app_functions.py) FILE

def display_raw_gsc_code(start, end):
    """ grab gsc 'example code' from raw file & display in output(frame2) area """
    f = open(join(dirname(realpath(__file__)), "raw_gsc_code.txt"), 'rt')
    with f as file:
        copy = False
        for line in file:
            if line.strip() == start:
                copy = True
                continue
            elif line.strip() == end:
                break
            elif copy:
                content_frame2_text_area.insert(tk.END, line)
    f.close()

TEXT WIDGET IN TK(app.py) FILE

content_frame2_text_area = Text(content_frame2, relief="ridge", bd=2) #GROOVE
content_frame2_text_area.grid(column=2, row=1, sticky="ns", padx=5, pady=5)
Asked By: phil

||

Answers:

You need to pass content_frame2_text_area as an argument of display_raw_gsc_code()

Answered By: phil
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.