Программа выдает ошибку:
NameError: name 'priceshop' is not defined
на 33 строчке.
Саму переменную я создавал ниже и в функции def она была объявлена глобальной. При этом переменную bar в функции def auto_bar() он не считает ошибкой. Предполагаю, что проблема в том, что сама переменная создается в def shop() и считается "локальной".
#библиотеки
from tkinter import *
from tkinter.ttk import Progressbar
#переменные
cnt = 0
cnt = int(cnt)
bar_cnt = 0
bar_cnt = int(bar_cnt)
pricePB_int = 10
pricePB_int = int(pricePB_int)
pricePB_str = pricePB_int
pricePB_str = str(pricePB_str)
#вызов функции клика
def clicked():
global cnt
cnt += 1
wtext.configure(text = "Печенье: " + str(cnt))
def auto_price():
global pricePB_int
global pricePB_str
global fgPS
global priceshop
if cnt < pricePB_int:
fgPS = '#FF0000'
else:
fgPS = "#000000"
priceshop.configure(text = "Цена: " + str(pricePB_str))
# вызов функции прогресс бара
def auto_bar():
global bar_cnt
global cnt
print('Progressbar')
if bar_cnt >= 100:
bar_cnt = 0
bar_cnt += 5
cnt += 1
print(">100")
else:
bar_cnt += 5
print("<100")
bar.configure(value = bar_cnt)
wtext.configure(text = "Печенье: " + str(cnt))
auto_price()
window.after(1000, auto_bar)
def buying():
print('buy')
#вызов магазина
def shop():
global pricePB_int
global pricePB_str
wshop = Toplevel()
wshop.resizable(False, False)
wshop.title("Магазин")
shopx = window.winfo_x() + ((window.winfo_width() / 2) - 320)
shopy = window.winfo_y() + ((window.winfo_height() / 2) - 240)
wshop.geometry('%dx%d+%d+%d' % (240, 144, shopx, shopy))
wshop.iconbitmap(r'shop.ico')
btnshop = Button(wshop, text = "Купить", bg = "#FF9900", activebackground="#FF6600", command = buying)
btnshop.place(anchor = "center", x = 120, y = 72)
priceshop = Label(wshop, text = "Цена: " + str(pricePB_str))
priceshop.place(anchor = "center", x = 120, y = 95)
priceshop['fg'] = fgPS
PBupgrage = Label(wshop, text = "Скорость работы: 1с" + " -> " + "1.2c")
PBupgrage.place(anchor = "center", x = 120, y = 45)
#параметры интерфейса
window = Tk() # создание окна
window.title("Кликер")
window.geometry('640x480')
window.resizable(False, False)
window.config(bg = "#35f29d")
window.iconbitmap(r'cookie.ico')
#счетчик
wtext = Label(window, text= "Печенье: " + str(cnt), bg = "#35f29d")
wtext.place(anchor = "n", x = 320, y = 180)
#кнопка
btn = Button(window, text="Клик", command = clicked, bg = '#FF9900', activebackground="#FF6600")
btn.place(anchor = 'center', x = 320, y = 220)
#прогресс бар
bar = Progressbar(window, length=200)
bar.place(anchor = "center", x = 320, y = 10, relwidth = .25)
bar['value'] = bar_cnt
#меню
menu = Menu(window)
menu.add_cascade(label='Магазин', command = shop)
window.config(menu=menu)
#запуск функции
auto_bar()
window.mainloop() # программа в ожидании действий