Python 每秒递增一个数+;1英寸Tkinter标签
我的问题是,while循环中的数字每秒钟都在增加。我在shell中找到了解决方案,但是“time.sleep()”函数在“Tkinter”上不起作用。请帮忙Python 每秒递增一个数+;1英寸Tkinter标签,python,tkinter,Python,Tkinter,我的问题是,while循环中的数字每秒钟都在增加。我在shell中找到了解决方案,但是“time.sleep()”函数在“Tkinter”上不起作用。请帮忙 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 i
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
您通常不希望在GUI程序中进行这样的睡眠,但请尝试以下方法:
while money < 300:
money += 1
time.sleep(1)
root.update()
当钱<300时:
货币+=1
时间。睡眠(1)
root.update()
root.after是tkinter与time.sleep的等价物,只是时间是毫秒而不是秒。有很多例子需要研究
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()
将tkinter作为tk导入
root=tk.tk()
钱=100
label=tk.label(root,text=str(money)+“$”)
label.grid()
def倒计时(货币):
货币+=1
标签['text']=str(货币)+“$”
如果货币<300:
根。之后(100,倒数,钱)
根。之后(100,倒数,钱)
root.mainloop()
可能重复感谢,我的朋友我一直在寻找一个好的解释,但至少我找到了一些对我有用的东西谢谢
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()