I am making a game using the customtkinter library the game is a clicker tycoon game that involves beans. quite similar to cookie clicker. below is the entire code
import customtkinter as ctk
from customtkinter import CTkImage,CTkButton,CTkLabel
#from PIL import Image
import time as t
# stores money
money = 0
# variables for bean generation
beans = 0
click = 1
#toggles bean collection
toggle=False
#Application
class BeanApp(ctk.CTk):
def __init__(self):
super().__init__()
# sets window and frames
self.title("Bean Tycoon")
self.geometry("500x500+700+200")
#click modes
multiplier_lbl=CTkLabel(self,text= "Multipliers")
multiplier_lbl.place(x=250,y=1,anchor="n",)
def xOne():#x1 multiplier toggle
global click
click=1
one_click_mode_btn.configure(state="disabled")
two_click_mode_btn.configure(state="enable")
click_multiplyer_lbl.configure(text=f" Beans/click: x{click} ")
def xTwo():#x2 multiplier toggle
global click
click=2
one_click_mode_btn.configure(state="enable")
two_click_mode_btn.configure(state="disabled")
click_multiplyer_lbl.configure(text=f" Beans/click: x{click} ")
one_click_mode_btn= CTkButton(self,text="x1",width=20,height=10,command=xOne,state="disabled")
one_click_mode_btn.place(x=145,y=25,anchor="nw")
two_click_mode_btn=CTkButton(self, text="x2",width=20,height=10,command=xTwo,state="disabled")
two_click_mode_btn.place(x=173,y=25,anchor="nw")
click_multiplyer_lbl=CTkLabel(self,text=f" Beans/click: x{click} ")
click_multiplyer_lbl.place(x=3,y=45,anchor="nw",)
# Bean generator
#beanbtn = CTkImage(Image.open("Pictures\TheBean.png"),size=(200,200))
bean_amt_lbl = CTkLabel(self,text= f" Beans: {beans} ",)
bean_amt_lbl.place(x=3,y=5,anchor="nw")
def clicked():
global beans
global click
beans += click
bean_amt_lbl.configure(text= f" Beans: {beans} ")
sell_beans_btn = CTkButton(self,text= "",image=None, command= clicked,width=180,height=180)
sell_beans_btn.place(x=250,y=330, anchor="s")
# Sell Beans
money_amt_lbl = CTkLabel(self,text=f" Money: ${money} ", )
money_amt_lbl.place(x=3,y=25,anchor='nw')
def sell():
global money
global beans
while beans >0:
money = money+beans
beans = 0
bean_amt_lbl.configure(text=f" Beans: {beans} ")
money_amt_lbl.configure(text=f" Money: ${money }")
sell_bean_btn = CTkButton(self,text="Sell Beans",image=None, command=sell)
sell_bean_btn.place(x=250,y=360,anchor='s')
#2 times multiplier
def d_b_u():#double_bean_upgrade
global click,money
if money>=100:
click=2
money=money-100
money_amt_lbl.configure(text=f" Money: ${money }")
double_bean_upgrade_btn.configure(state="disabled")
two_click_mode_btn.configure(state="disabled")
one_click_mode_btn.configure(state="enable")
click_multiplyer_lbl.configure(text=f" Beans/click: x{click} ")
#automatic bean collector
def a_b_c_u():#automatic_bean_collector_upgrade
global beans,money,toggle
if money>=20:
money=money-20
money_amt_lbl.configure(text=f" Money: ${money }")
auto_collect_bean_btn.configure(state="disabled")
while beans >=0 :
beans=beans+1
bean_amt_lbl.configure(text=f" Beans: {beans} " )
t.sleep(3)
print(beans)
#Shop
shop_lbl= CTkLabel(self,text="Shop")
shop_lbl.place(x=425,y=5,anchor="nw")
double_bean_upgrade_btn = CTkButton(self,text="Bean Doubler\n$100",command=d_b_u,width=20,corner_radius=20)
double_bean_upgrade_btn.place(x=390,y=30,anchor="nw")
auto_collect_bean_btn = CTkButton(self,text="Auto Collect 1\n$200",command=a_b_c_u,width=20,corner_radius=20)
auto_collect_bean_btn.place(x=390,y=70,anchor="nw")
if __name__ == "__main__":
app = BeanApp()
app.mainloop()
the game involves an incremental counter that runs in the background, the loop is toggled via a button. however when the loop activates it freezes the application. how can this be fixed?
EDIT: I hashtagged the image library and image function since it does not effect the code in the slightest as of now.
EDIT 2:How do i properly implement Threads? i dont think i did this right...
#automatic bean collector
def b_c_():
global beans
while beans >=0 :
beans=beans+1
bean_amt_lbl.configure(text=f" Beans: {beans} " )
t.sleep(3)
print(beans)
bean_collector=Thread(target= b_c_)
def a_b_c_u():#automatic_bean_collector_upgrade
global beans,money,toggle
if money>=20:
money=money-20
money_amt_lbl.configure(text=f" Money: ${money }")
auto_collect_bean_btn.configure(state="disabled")
bean_collector.run()
Edit 3: Removed as it strayed from the original problem