Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update python tkinter window

Tags:

python

tkinter

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!

like image 998
Ramon Geessink Avatar asked Jul 17 '26 15:07

Ramon Geessink


2 Answers

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()
like image 194
fhdrsdg Avatar answered Jul 19 '26 06:07

fhdrsdg


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

like image 39
guest Avatar answered Jul 19 '26 07:07

guest



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!