I'm creating simple notepad program in Tkinter. I decided to put functions in separate file. Is it possible if the functions are operating on variables declared in main file? This is snippet of the code: main.py
from tkinter import *
from otherfile import cut
root = Tk()
....
menu_edit.add_command(label='Cut', compound='left', command=cut)
...
main_text = Text(root, wrap ='word')
main_text.pack(expand='yes', fill = 'both')
now I have otherfile.py
def cut():
main_text.event_generate('<<Cut>>')
return 'break'
Once I run it I'll get: Exception in Tkinter callback
Traceback (most recent call last):
File "C:...\tkinter\__init__.py", line 1699, in __call__
return self.func(*args)
File "C:\....otherfile.py", line 3, in cut
main_text.event_generate('<<Cut>>')
NameError: name 'main_text' is not defined
So I guess otherfile.py does not understand main_text which is defined in main.py. Is there a way to bypass it and allow me to put all the functions in different py file?
from main import main_textat the top of your otherfile.py?