I am making an incremental game (e.g. Cookie Clicker-style games) in Python, which is a still a work-in-progress.
from Tkinter import *
import time
master = Tk()
n = int(0)
inc = int(1)
money = int(0)
autoclick = int(0)
def deduction(): # 1 autoclick is $20, deductions
global money, autoclick
money = money - 20
autoclick = autoclick + 1
automoney()
def automoney(): # Increases money every second
global money, autoclick
money = money + autoclick
print("+" + str(autoclick) + " money!")
time.sleep(1)
automoney()
def printmoney(): # Checks how much money you have
print('Your balance is ' + str(money) + ' dollars.')
def collectmoney(): # Increases money every click
global n, inc, money
n = n + inc
print('+' + str(n) + ' money!')
money = money + n
n = n - inc
def checkauto(): # Checks how much Auto-Clickers you have
global autoclick
print('You have ' + str(autoclick) + ' Auto Clickers.')
button1 = Button(master, text='Cash!', command=collectmoney)
button1.pack()
checkbutton1 = Button(master, text='Check Cash', command=printmoney)
checkbutton1.pack()
incbutton1 = Button(master, text='Auto Clicker', command=deduction)
incbutton1.pack()
checkbutton2 = Button(master, text='Check Auto Clickers', command=checkauto)
checkbutton2.pack()
mainloop()
... it works, but Tkinter crashes when I press the button Auto Clicker (probably due to the infinite loop).
I followed the instructions in this, and changed some of the code to this:
def automoney():
money.set(money.get() + autoinc.get())
incbutton1.after(1000, automoney)
incbutton1.after(1000, automoney)
incbutton1.mainloop()
... which didn't work.
Is there any way to fix the button crashing, while still doing everything it's meant to do?