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()
在额外收入部分,我想将某种值 m 添加到counter1
,但我不知道如何做到这一点。
答案 0 :(得分:2)
要在tkinter应用中每秒运行一次函数,请使用after
。
之后(delay_ms,callback = None,* args)
注册在给定时间后调用的警报回调。
应该在要定期调用的函数的末尾调用此方法。例如,设想一个MyWidget
类,我想每秒运行foo
方法:
class MyWidget(tk.Widget):
def foo(self):
print("foo")
self.after(1000, self.foo)