【问题标题】:How do I make my function to run every second in python? [duplicate]如何让我的函数在 python 中每秒运行一次? [复制]
【发布时间】:2017-10-19 12:37:19
【问题描述】:
from Tkinter import*
from tkMessageBox import*
import random
import time

n = 1
a = 1

class GameFrame(Frame):

    def __init__(self):
        global a
        global n
        Frame.__init__(self)
        self.master.title("Be a Billionaire")
        self.master.geometry("1200x700")
        self.master.resizable(0,0)
        self.grid()

        self._mission = Label(self, text = "Your Mission is to Earn 1,000,000,000$" ,font = ("Arial", 30, "bold"))
        self._mission.grid()

        counter1 = IntVar()
        counter1.set(0)

        self._currentmoney = Label(self, text = "Your Current Money:" ,font = ("Arial", 30, "bold"))
        self._currentmoney.grid()

        self._currentmoney = Label(self, textvariable = counter1 ,font = ("Arial", 30, "bold"))
        self._currentmoney.grid()

        ###########################################################Click###########################################################
        self._moneyearn = Button(self, text = "Click Me to Earn Money", font = ("Arial", 30, "bold"), command = lambda: increasemoney(a))
        self._moneyearn.grid()

        self._clickupgrade1 = Button(self, text = "Click Me to Upgrade Click", font = ("Arial", 15, "bold"), command = lambda: upgradeclick())
        self._clickupgrade1.grid()

        self._costlabel1 = Label(self, text = "Cost:", font = ("Arial", 15, "bold"))
        self._costlabel1.grid()

        counter2 = IntVar()
        counter2.set(2)
        self._neededmoney1 = Label(self, textvariable = counter2, font = ("Arial", 15, "bold"))
        self._neededmoney1.grid()

        def increasemoney(x):
            counter1.set(counter1.get() + x)

        def upgradeclick():
            global a
            global n
            if counter1.get() >= n*(n+1)*(n+2):
                a += 5*n
                counter1.set(counter1.get() - n*(n+1)*(n+3))
                counter2.set((n+1)*(n+2)*(n+3))
                n += 1               
            else:
                showwarning(message = "You Don't Have Enough Money!", parent = self)        
        ###########################################################################################################################

        ##########################################################Lottery##########################################################
        self._lotterytitle = Label(self, text = "LOTTERY! : You can win 0$ ~ 100*(money you put in)", font = ("Arial", 15, "bold"))
        self._lotterytitle.grid()

        self._lotterymoney = IntVar()
        self._lotteryentry = Entry(self, textvariable = self._lotterymoney, font = ("Arial", 15, "bold"))
        self._lotteryentry.grid()

        self._lotterybutton = Button(self, text = "See the results", font = ("Arial", 15, "bold"), command = lambda: lottery())
        self._lotterybutton.grid()

        def lottery():
            x = self._lotterymoney.get()
            if x <= counter1.get():
                l = random.randint(1, 100)
                if 100 >= l > 99:
                    counter1.set(counter1.get() + x*99)
                    showinfo(message = "Congratulations! You've won First Prize(100*(Money you have put in))", parent = self)
                elif 99 >= l > 94:
                    counter1.set(counter1.get() + x*29)
                    showinfo(message = "Congratulations! You've won Second Prize(20*(Money you have put in))", parent = self)
                elif 94 >= l > 80:
                    counter1.set(counter1.get() + x*9)
                    showinfo(message = "Congratulations! You've won Third Prize(10*(Money you have put in))", parent = self)
                else:
                    counter1.set(counter1.get() - x)
                    showinfo(message = "Sorry! You've Lost", parent = self)
            else:
                showwarning(message = "You Don't Have Enough Money!", parent = self)
        ###########################################################################################################################

        ########################################Additional Income(Earns Money Every Second)########################################
        self._additionaltitle = Label(self, text = "Additional Income(Earns Money Every Second)", font = ("Arial", 15, "bold"))
        self._additionaltitle.grid()




def main():
    GameFrame().mainloop()

main()

额外收入部分,我想为counter1添加某种价值m,但我不知道该怎么做。

【问题讨论】:

  • 你有一个函数increasemoney 似乎正是这样做的。为什么不能用?
  • 您实际上不需要发布所有代码来询问如何每秒运行一次函数。后者的答案是使用从time 模块导入的sleep(1)
  • @Ev.Kounis 在事件驱动的 GUI 循环中调用time.sleep 通常不是一个好主意,因为这会冻结循环。因此,此类 GUI 框架提供了其他请求延迟的方式; Tkinter 中的常用方法是调用小部件的 .after 方法,如右腿的答案所示。
  • 您可能会发现this answer 中的代码很有帮助。

标签: python tkinter


【解决方案1】:

要让函数在 tkinter 应用程序中每秒运行一次,请使用 after

after(delay_ms, callback=None, *args)

注册一个在给定时间后调用的警报回调。

这个方法应该在被周期性调用的函数结束时调用。例如,想象一个MyWidget 类,我想每秒运行一次foo 方法:

class MyWidget(tk.Widget):
    def foo(self):
        print("foo")
        self.after(1000, self.foo)

【讨论】:

  • @PM2Ring 你说得对,我编辑了它:)
  • 这样更好!可惜我不能再给你一个赞成票。 ;) 我一直在寻找一个很好的规范问题来用作这个问题的欺骗目标,但我运气不佳。
  • @PM2Ring 好吧,stackoverflow.com/questions/25753632/… 确实说明了一切,但不是很规范。我会做一个,但我可能会被标记为骗子^^ 不过会试一试,因为它看起来可能有用
  • 是的,那是我看到的第一个,但我认为这不是一个很好的匹配,因为 OP 已经知道.after,他们只是没有正确使用它。如果您想将规范问题作为自我回答的问题,请确保该问题正确符合 SO 标准,并且您应该没问题,尽管您可能需要添加注释来解释它是针对规范问题的。完成后在此处或在 Python 聊天室中向我发送消息。
  • @PM2Ring 我发布了关于这个主题的规范问答:stackoverflow.com/questions/44085554/…
猜你喜欢
  • 1970-01-01
  • 2011-08-04
  • 1970-01-01
  • 1970-01-01
  • 2020-07-15
  • 2017-02-03
  • 2013-11-15
  • 1970-01-01
  • 2012-01-09
相关资源
最近更新 更多