Python Forum
[Tkinter] Redirecting all print statements from all functions inside a class to Tkinter - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] Redirecting all print statements from all functions inside a class to Tkinter (/thread-33417.html)



Redirecting all print statements from all functions inside a class to Tkinter - Anan - Apr-24-2021

I have written the below code for displaying some content on the 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__":



RE: Redirecting all print statements from all functions inside a class to Tkinter - ndc85430 - Apr-24-2021

Why can't you move the function out of where it is? Because it's a closure? Just have the function take the things it needs as arguments, rather than expecting them to be in the enclosing scope.