Reputation: 13
I wanted to make a game like a clicker, where coding brings money, but I ran into this error. The error seems to be due to the fact that 'coding' is in another function, but I don't understand how I can fix it. Please help me. I will be very grateful to you
from tkinter import *
from time import sleep
from threading import *
from tkinter import ttk
from tkinter import messagebox
button_track = False
button_code = True
wait_code = 30
def check_code():
global button_code
if button_code == True:
money.set(money.get() + 50)
button_code = False
def buy_klava_func():
if money.get() >= 50:
money.set(money.get() - 50)
buy_klava.grid_remove()
coding = Button(root, text='Написать код', command=check_code)
coding.grid(column=0, row=0)
def timer_code():
global button_code
global wait_code
while True:
if button_code == False:
coding['state'] = 'disabled'
sleep(wait_code)
coding['state'] = 'active'
button_code = True
root = Tk()
root.geometry('900x900')
money = IntVar()
money.set(50)
buy_klava = Button(root, text='Купить клавиатуру (50 монет)', command=buy_klava_func)
buy_klava.grid(column=0, row=0)
th_timer_code = Thread(target=timer_code)
th_timer_code.start()
root.mainloop()
Upvotes: 1
Views: 58
Reputation: 364
Looks like coding
is not a global variable. Declare it above all the functions, so that you can use it within all functions.
from tkinter import *
from time import sleep
from threading import *
from tkinter import ttk
from tkinter import messagebox
button_track = False
button_code = True
wait_code = 30
def check_code():
global button_code
if button_code == True:
money.set(money.get() + 50)
button_code = False
def buy_klava_func():
global coding # <=== define coding as global
if money.get() >= 50:
money.set(money.get() - 50)
buy_klava.grid_remove()
coding = Button(root, text='Написать код', command=check_code)
coding.grid(column=0, row=0)
def timer_code():
global button_code
global wait_code
global coding
while True:
if button_code == False:
coding['state'] = 'disabled'
sleep(wait_code)
coding['state'] = 'active'
button_code = True
root = Tk()
root.geometry('900x900')
money = IntVar()
money.set(50)
buy_klava = Button(root, text='Купить клавиатуру (50 монет)', command=buy_klava_func)
buy_klava.grid(column=0, row=0)
th_timer_code = Thread(target=timer_code)
th_timer_code.start()
root.mainloop()
Upvotes: 1
Reputation: 46751
It is because coding
is a local variable, so it cannot be accessed inside timer_code()
.
Since you just replace buy_klava
by coding
, my suggestion is simply to configure buy_klava
instead of removing it and creating coding
:
def buy_klava_func():
if money.get() >= 50:
money.set(money.get() - 50)
"""
buy_klava.grid_remove()
coding = Button(root, text='Написать код', command=check_code)
coding.grid(column=0, row=0)
"""
# just configure "buy_klava" instead of removing it and create "coding"
buy_klava.config(text='Написать код', command=check_code)
def timer_code():
global button_code
#global wait_code
while True:
if button_code == False:
buy_klava['state'] = 'disabled' # update buy_klava
sleep(wait_code)
buy_klava['state'] = 'active' # update buy_klava
button_code = True
Upvotes: 0