1

I have written the below code for displaying some content on GUI window. Some of the code I have removed from between which was not needed here. _print(text) function is used to print the content on the gui screen. Can someone tell me how I can make this function globally accessible so that _print statements in other functions of the same class also get updated on the GUI.

I cannot move this _print function outside of GUI() function.
Basically i need all my print statements to be pushed on tkinter. How do I do that?

   def accept_client_connections(master=None):
      try:
         while True:
            # some code
        _print("This statement needs to be printed on tkinter")


    def GUI():
    ***def _print(text):
        tex.insert(END, text + '\n')
        tex.see("end")***   

    window = Tk()
    #some code

    tex = Text(window,width=80, height=40, font=('arial',10,'bold'), wrap=WORD)
    tex.pack(side=TOP, fill=X,padx=10,pady=5)
    
    messagebox.showinfo("","Waiting")
    _print('xvdgfd...')



if __name__ == "__main__": 
  
Madhav
  • 35
  • 11
  • 1
    I think this [post] should solve your problem. [1]https://stackoverflow.com/questions/17395338/how-to-access-a-function-inside-a-function#:~:text=Perhaps%20just%20calling%20make_adder(),you%20can%20call%20as%20well. – Michael Stevens Apr 24 '21 at 02:47
  • 1
    _"I cannot move this _print function outside of GUI() function."_ - why can't you? What happens when you do? – Bryan Oakley Apr 24 '21 at 03:24
  • 1
    @BryanOakley the contents inside _print() can only be printed once GUI is called. tex is called when tkinter window gets created. Even if i try to use it outside, it gives compilation error that tex is an undefined variable – Madhav Apr 24 '21 at 03:33
  • 1
    Have you looked into classes and OOP. – Delrius Euphoria Apr 24 '21 at 06:01
  • Please fix the indentation first. – Delrius Euphoria Apr 24 '21 at 06:03
  • 2
    @CoolCloud yes classes did help me in solving the problem. thank you so much.... – Madhav Apr 25 '21 at 19:30

3 Answers3

1

I don't think you can do that, cause nested functions have the local scope and they are used as helper functions. instead, you can pass text argument to the outer function then use it within the inner function, here is how your code will look like.

def GUI(text):
    def _print():
        tex.insert(END, text + '\n')
        tex.see("end")
    _print()

# then call GUI function not _print
GUI('this is a text...')
window.mainloop()
1

This might not be the best solution but this is how I figured out.

I have been able to solve this problem by making use of Classes. Both the functions were included in the class and using global variable under GUI() helped in making the tex variable globally accessible.

Madhav
  • 35
  • 11
0

You can do it like that:

def GUI():
    def _print(text):
        tex.insert(END, text + '\n')
        tex.see("end")
    return _print
# some code here...
_print = GUI()
_print("1234567890")
Vad Sim
  • 266
  • 8
  • 21
  • I can get the statements inside the GUI() printed on Tkinter. but the statements which are in another function, I am not able to print them. I have edited my ques for better understanding – Madhav Apr 24 '21 at 04:42