在 Python 中每秒更新一个变量? [英] Update a variable every second in Python?

查看:36
本文介绍了在 Python 中每秒更新一个变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Python 编写一个非常简单的增量游戏(最终产品几乎肯定是少于 5000 行),我遇到了一个问题.到目前为止,所有货币都是通过单击添加的,但是我添加的下一个升级将每秒向主要货币添加某个数字.应该注意的是,我为此将 Tkinter 与 Python 一起使用,所以我的变量都是 IntVar() 的.如果我将当前货币设置为 500,并且将 cps(每秒货币)设置为 1,我如何将 1 添加到我的 currency = IntVar() 每秒?

I'm writing a VERY simple incremental game in Python (final product will almost certainly be less than 5000 lines), and I've come to a problem. So far, all the currency is added solely by clicking, but the next upgrade I'm adding is going to add a certain number to the main currency every second. It should be noted that I'm using Tkinter with Python for this, so my variables are all IntVar()'s. If I have the current currency set to 500, and the cps (currency per second) to 1, how do I add 1 to my currency = IntVar() every second?

currency = IntVar()
currency.set(500)
cps = IntVar()
cps.set(1)

我还假设(可能是错误的)它将是一个 while 循环,并且我可能会有 import timetime.sleep(1) 在某处.非常感谢任何帮助!

I'm also assuming (possibly incorrectly) that it'll be a while loop and that I'll probably have import time and time.sleep(1) in there somewhere. Any help is much appreciated!

推荐答案

假设你有一个变量 root 作为你的主窗口:

Let's assume you have a variable root as your main window:

def every_second():
    global currency, cps, root
    currency.set(currency.get() + cps.get())
    root.after(1000, every_second)

root.after(1000, every_second)
root.mainloop()

这篇关于在 Python 中每秒更新一个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆