Tk: how to call it just to display something and return to the main program?

Sorry for the noob question, but I really don't get it.

I am using python / tkinter and I want to display something (say a canvas with several shapes on it) and save it until the program terminates. I understand that no widgets will be displayed until I call tkinter.tk.mainloop(). However, if I call tkinter.tk.mainloop(), I cannot do anything until the user closes the main window.

I do not need to track any user input events, just show some things. What a good way to do this without taking control away mainloop?

EDIT:

Is this example code reasonable:

class App(tk.Tk):
  def __init__(self, sim):
    self.sim = sim # link to the simulation instance
    self.loop()

  def loop():
    self.redraw() # update all the GUI to reflect new simulation state
    sim.next_step() # advance simulation another step
    self.after(0, self.loop)

  def redraw():
    # get whatever we need from self.sim, and put it on the screen

EDIT2 (added after_idle):

class App(tk.Tk):
  def __init__(self, sim):
    self.sim = sim # link to the simulation instance
    self.after_idle(self.preloop)

  def preloop():
    self.after(0, self.loop)

  def loop():
    self.redraw() # update all the GUI to reflect new simulation state
    sim.next_step() # advance simulation another step
    self.after_idle(self.preloop)

  def redraw():
    # get whatever we need from self.sim, and put it on the screen
+3
source
2

Tk , . , , , , . , .

. -, . - - , . , .

- . , while True, . , . , , , after, . after , , GUI .

+3

GUI tk , . . :

Tkinter:

0

Source: https://habr.com/ru/post/1788124/


All Articles