Python Forum
[Tkinter] Redirecting all print statements from all functions inside a class to Tkinter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Redirecting all print statements from all functions inside a class to Tkinter
#1
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__":
Yoriz write Apr-24-2021, 08:11 AM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using Tkinter inside function not working Ensaimadeta 5 4,860 Dec-03-2023, 01:50 PM
Last Post: deanhystad
  tkinter destroy label inside labelFrame Nick_tkinter 3 4,480 Sep-17-2023, 03:38 PM
Last Post: munirashraf9821
  [Tkinter] Help running a loop inside a tkinter frame Konstantin23 3 1,449 Aug-10-2023, 11:41 AM
Last Post: Konstantin23
Lightbulb Using Tkinter With Concurrent.Futures / ThreadPoolExecutor Class AaronCatolico1 1 1,418 Dec-14-2022, 08:01 PM
Last Post: deanhystad
Lightbulb [Tkinter] Tkinter Class Import Module Issue AaronCatolico1 6 2,971 Sep-06-2022, 03:37 PM
Last Post: AaronCatolico1
  [Kivy] Acces atributes from a button defined in a class inside the .kv Tomli 2 2,052 Jun-10-2021, 01:05 AM
Last Post: Tomli
  tkinter moving an class object with keybinds gr3yali3n 5 3,183 Feb-10-2021, 09:13 PM
Last Post: deanhystad
  Help with tkinter gui and functions Omer_ 0 1,494 Sep-22-2020, 11:43 AM
Last Post: Omer_
  [Tkinter] How to Print a list = ['a','b','c'], using tkinter along with a custom font? Pleiades 2 2,319 Sep-15-2020, 03:54 PM
Last Post: Pleiades
  [Tkinter] Troubles with accessing attr from other class zarize 3 2,576 Aug-20-2020, 06:05 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020