-1

I have two Python files, test51.py and test61.py

The first file has a widget with a button and text box The second file has a couple of functions to insert text to the textbox.

When I run, I am getting "T" is not defined - T is the textbox's name.

How may I make the text box visible in the second file?

First file; test51.py

#!/usr/bin/python3
from tkinter import *
from test61 import *

def main():
    root = Tk()
    T = Text(root, height=2, width=30)
    T.pack()
    T.insert("1.0", "Just a text Widget\nin two lines\n")
    MyButton = Button(root, text = 'Press Me', command = lambda: test())
    MyButton.pack()
    root.mainloop()

if __name__ == "__main__":
  main()

Second file; test61.py

#!/usr/bin/python3
from tkinter import *
from test51 import *
import time
def test():
    delay = 3.0
    time.sleep(delay)
    print_to_gui('Files currently transferring')
    time.sleep(delay)
    print_to_gui('Currently merging all pdfs')

def print_to_gui(quote):
    T.insert("1.0", quote)
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
HaNas
  • 25
  • 4
  • Read up on [Tutorial - 9.2. Python Scopes and Namespaces](https://docs.python.org/3/tutorial/classes.html#python-scopes-and-namespaces) – stovfl Apr 27 '20 at 07:12

1 Answers1

0

T isn't defined in the test61.py file, define T as global in main(). You can read this for more about how to use variables across files in Python.

This should work:

First file; test51.py

#!/usr/bin/python3
from tkinter import *
import test61

def main():
    root = Tk()
    global T
    T = Text(root, height=2, width=30)
    T.pack()
    T.insert("1.0", "Just a text Widget\nin two lines\n")
    MyButton = Button(root, text = 'Press Me', command = lambda: test61.test())
    MyButton.pack()
    root.mainloop()

if __name__ == "__main__":
    main()

Second file; test61.py

from tkinter import *
import test51
import time

def test():
    delay = 3.0
    time.sleep(delay)
    print_to_gui('Files currently transferring')
    time.sleep(delay)
    print_to_gui('Currently merging all pdfs')

def print_to_gui(quote):
    test51.T.insert("1.0", quote)

if __name__ == "__main__":
    test51.main()
faressalem
  • 574
  • 6
  • 20
  • @HaNas Oh I've named the files differently, fixed it. – faressalem Apr 27 '20 at 03:25
  • 1
    Thank you, but with your correction, still I am getting "name t2 is not defined. – HaNas Apr 27 '20 at 23:50
  • OMG :D @HaNas, changing file names is so boring, whatever, in python when you import some module, you need to use its name to use something that's defined inside that module, so to call `test()` which is defined inside `test61.py`, you call it like this `test61.test()`. – faressalem Apr 28 '20 at 01:09
  • Thank you. It does work now but not the way that I expected. the text box gets updated after the end of the text function. Is it possible to update the text box after each print? – HaNas Apr 29 '20 at 14:32
  • I fixed it by updaiting the widget using test51.root.update() – HaNas Apr 29 '20 at 21:07