1

My problem is, increasing numbers in while loop by every second. I have found the solution in shell, but "time.sleep()" function doesn't work on "Tkinter". Please help!

import time
from tkinter import *

root = Tk()
root.configure(background="grey")
root.geometry("500x500")

#I want to increase money in label every one second  +1  which is displayed,
Money = 100
etiket1 = Label(root,text = str(money)+"$",fg = "Green")
etiket1.pack()

while money < 300:
   money += 1
   time.sleep(1)
   if money == 300:
        break    

#"while" loop doesn't work with "time.sleep()" in tkinter

root.mainloop()
Özden
  • 35
  • 6
  • Possible duplicate of [How to create a timer using tkinter?](http://stackoverflow.com/questions/2400262/how-to-create-a-timer-using-tkinter) – Bryan Oakley Feb 27 '16 at 13:38

2 Answers2

1

You wouldn't normally want to be doing a sleep like that in a GUI program, but try this:

while money < 300:
    money += 1
    time.sleep(1)
    root.update()
Ed Randall
  • 6,887
  • 2
  • 50
  • 45
1

root.after is the tkinter equivalent of time.sleep, except that time is milliseconds instead of seconds. There are multiple examples on SO to study.

import tkinter as tk
root = tk.Tk()

money = 100
label = tk.Label(root, text = str(money)+"$")
label.grid()

def countup(money):
    money += 1
    label['text'] = str(money)+"$"
    if money < 300:
        root.after(100, countup, money)

root.after(100, countup, money)
root.mainloop()
Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52
  • Thanks,my friend I've been looking for a good explanation,but al least I've found something ,that worked for me thank you – Özden Feb 28 '16 at 13:31