Home > OS >  (SOLVED) having trouble inserting text via pythons .insert() method to a tkinter Text() widget that
(SOLVED) having trouble inserting text via pythons .insert() method to a tkinter Text() widget that

Time:03-20

so 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 im getting errors.

Heres the error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Phil-\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
    return self.func(*args)
  File "c:\Users\Phil-\python_main\gsc script building app\app.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:\Users\Phil-\python_main\gsc script building app\app_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 im 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)

CodePudding user response:

You need to pass content_frame2_text_area as an argument of display_raw_gsc_code(). – acw1668 Mar 16 at 0:49

  • Related