7

I'm currently trying to update a TKinter window every second. So the idea is that is should open a window, let python updates the field, show updated window. The situation right now is that the second window is only showing when i close the first one. I'm guessing this has something to do with mainloop(). I looked into .update() and .update_idletasks() but I can't figure out how to implement it. TKinter is used to show a field with houses in it. So in general is should do this:

  • Generate house location (already implemented)
  • Show field with houses in it (already implemented)
  • Generate new location of houses (already implemented)
  • Show updated field

This is my current code. I'm not sure if the update function is necessary.

    class Plot(tk.Frame):
        def __init__(self, master=None):
            tk.Frame.__init__(self, master)
            self.grid()

        def createWidgets(self, list_houses):
            houses = list_houses
            self.w = tk.Canvas(self, width = field.width*SIZE, height = field.height*SIZE, bg = '#E4E4E4')

            ...

            self.w.grid()

        def update(self):
            ?

#GENERATES FIELD AND HOUSES
...

#PRINT FIRST WINDOW
plot = Plot()
plot.createWidgets(houses) <- PUT HOUSES IN TK INTER
plot.master.title('map of houses')
plot.mainloop()

# UPDATE FIELD <- THIS PART IS ONLY EXECUTED WHEN I CLOSE THE FIRST WINDOW, WHY?
i = 0
while i < 2:
    update = field.update_houses(houses) <- GENERATES NEW LOCATION OF HOUSES

    #PRINT UPDATED WINDOW, IT SHOULD BE PRINTED IN THE SAME WINDOW!
    plot = Plot()
    plot.createWidgets(houses) <- PUT HOUSES IN TKINTER
    plot.master.title('map of houses')

    i += 1

Thanks in advance!

2 Answers 2

9

Your update field is only executed when you close the window because tkinter's mainloop keeps running until the window is closed. Because of this mainloop, using a (long) while loop in tkinter is a bad idea, because the while loop locks up the tkinter mainloop.

To do something multiple times without blocking the mainloop, use the after method. This calls a function after a certain amount of time. You can call it once before entering your mainloop and then call it again in your update function, so that the update function calls itself. Take a look at this small example which uses after to execute an update function every second:

import Tkinter as tk
import random

def update():
    l.config(text=str(random.random()))
    root.after(1000, update)

root = tk.Tk()
l = tk.Label(text='0')
l.pack()
root.after(1000, update)
root.mainloop()
Sign up to request clarification or add additional context in comments.

Comments

7

you could use the .update to the window. That would make it update the window and allow everything to be on it.

1 Comment

Thank you for the great answer! plot.update() solves the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.