0

This is my code:

from tkinter import *
import time

count = 0
while count >= 0 :
    print(count)
    time.sleep(1)
    count = count + 1


root= Tk()


label1= Label(root, text='Gegevens', bg='lightblue')
label2= Label(root, text='Voortgang')
labelfiets_prestatie= Label (root, text='Fiets_Prestatie',)
labelStappen= Label(root, text='Meter', command= count, bg='red')


label1.pack(fill= X)
label2.pack()
labelfiets_prestatie.pack()
labelStappen.pack()


root.mainloop()

Now my problem is that whenever I delete the count code the GUI appears, but I want the numbers that are generated to be seen in the GUI. Where did it go wrong?

Yasin
  • 3
  • 2

1 Answers1

0

I think you should use after method.

This method registers a callback function that will be called after a given number of milliseconds. Tkinter guarantees that the callback will not be called earlier than that.

class App:
    def __init__(self, master):
        self.master = master
        self.poll() # start polling

    def poll(self):
        # do something 
        self.master.after(100, self.poll)

You can check out Tkinter after method.And if you want to update label ,you can use update_idletasks:

from tkinter import *
from time import sleep
root = Tk()
var = StringVar()   
l = Label(root, textvariable = var)
l.pack()

count=10
while count>=0:
    sleep(1)
    var.set(count)
    count=count-1
    root.update_idletasks()

Hope this helps.

McGrady
  • 10,869
  • 13
  • 47
  • 69
  • There is also an example of how to use it to make a simple timer in the docmentation http://stackoverflow.com/documentation/tkinter/6724/delaying-a-function#t=201701301400175181844. – j_4321 Jan 30 '17 at 14:02