0
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()

At the Additional Income part, I would like to add some kind of value m to counter1, but I don't know how to do it.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • You've got a function `increasemoney` which seems to do precisely this. Why can't you use it? – mkrieger1 May 19 '17 at 11:41
  • 3
    You do not really need to post all that code to ask *how to run a function once every second*. The answer to the latter would be by using `sleep(1)` imported from the `time` module – Ma0 May 19 '17 at 11:43
  • 1
    @Ev.Kounis It's generally _not_ a good idea to call `time.sleep` in an event-driven GUI loop because that freezes the loop. So such GUI frameworks provide other ways of requesting delays; the usual way in Tkinter is to call a widget's `.after` method, as illustrated in Right leg's answer. – PM 2Ring May 19 '17 at 12:02
  • You may find the code in [this answer](http://stackoverflow.com/a/29901175/4014959) helpful. – PM 2Ring May 19 '17 at 12:35

1 Answers1

2

To make a function run every second in a tkinter app, use after.

after(delay_ms, callback=None, *args)

Registers an alarm callback that is called after a given time.

This method should be called at the end of the function that is to be periodically called. For instance, imagine a MyWidget class whose I want to run the foo method every second:

class MyWidget(tk.Widget):
    def foo(self):
        print("foo")
        self.after(1000, self.foo)
Community
  • 1
  • 1
Right leg
  • 16,080
  • 7
  • 48
  • 81
  • @PM2Ring You're right, I edited it :) – Right leg May 19 '17 at 12:29
  • That's better! Pity I can't give you another upvote. ;) I've been looking for a good canonical question to use as a dupe-target for this one, but I'm not having much luck. – PM 2Ring May 19 '17 at 12:32
  • @PM2Ring Well, http://stackoverflow.com/questions/25753632/tkinter-how-to-use-after-method surely says it all, but isn't quite canonical. I would make one, but I probably would be flagged as a dupe ^^ Will give it a shot though, since it looks like it might be useful – Right leg May 19 '17 at 12:39
  • 1
    Yeah, that one was among the first that I looked at, but I decided it wasn't a great match because the OP already knew about `.after`, they just weren't using it properly. If you want to make a canonical as a self-answered question, make sure that the question properly conforms to the SO standard, and you should be ok, although you may want to add a comment explaining that it's for a canonical question. Ping me when it's done, either here or in the Python Chat room. – PM 2Ring May 19 '17 at 12:47
  • @PM2Ring I posted a canonical Q/A on this topic: http://stackoverflow.com/questions/44085554/how-to-use-the-after-method-to-make-a-periodical-call – Right leg May 20 '17 at 12:00