помогите зациклить игру

Ilya+

Новичок
Пользователь
Июл 3, 2022
1
0
1
Выдает ошибку после того как одолею монстра , заново не начинается. MacOS Monterey, Python 3.10. Pycharm

Код:
Exception in Tkinter callback
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/tkinter/__init__.py", line 1921, in __call__
    return self.func(*args)
  File "/Users/ilya/PycharmProjects/pythonProject3/main.py", line 53, in options3
    lbl.configure(text="Монcтр - циклоп")
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/tkinter/__init__.py", line 1675, in configure
    return self._configure('configure', cnf, kw)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/tkinter/__init__.py", line 1665, in _configure
    self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: invalid command name ".!label"


Python:
from tkinter import *
import random


class warrior:
    hp = 100
    damage = random.randrange(8,12)
    xp = 0
    coins = 0
class ork:
    hp = random.randrange(100,180)
    damage = random.randrange(5,8)
class troll:
    hp = random.randrange(39,57)
    damage = random.randrange(3,7)
class ticlop:
    hp = random.randrange(200,300)
    damage = random.randrange(6,12)


tes = 0

def clicked1():
    res = "Привет {}".format(txt.get())
    lbl.configure(text=res)
    txt.grid_forget()
    btn.configure(text="начать игру", command=clicked2)


def clicked2():
    lbl.grid_remove()
    lbl.configure(text="Приключение началось!")
    lbl.grid(column=0, row=0)
    btn.configure(text="в путь!", command=options3)


def options3():
    global tes
    tes = 0
    if tes != -5:
        tes = random.randint(1, 5)
    if tes == 1:
        lbl.configure(text="пока ничего..")
    elif tes == 2:
        lbl.configure(text="в пути")
    elif tes == 3:
        lbl.configure(text="Монстр - злобный троль")
        btn.configure(text="начать бой", command=PreFight)
    elif tes == 4:
        lbl.configure(text="Монтср - зеленый орк")
        btn.configure(text="начать бой", command=PreFight)
    elif tes == 5:
        lbl.configure(text="Монcтр - циклоп")
        btn.configure(text="начать бой", command=PreFight)

# START
root = Tk()
root.title("Рпг дохлая кляча")
root.geometry('800x400')
lbl = Label(root, text="Укажите имя персонажа")
lbl.grid(column=0, row=0)
txt = Entry(root, width=10)
txt.grid(column=1, row=0)
btn = Button(root, text="Готово!", command=clicked1)
btn.grid(column=0, row=1)

lbl2 = Label(root, text="Монеты"+"  "+ str(warrior.coins))
lbl2.grid(column=8, row=0)


def PreFight():
    global btn2
    global btn3
    global btn4
    global btn5
    global enemy
    global root2
    if tes >= 3:
        root2 = Tk()
        root2.title("fight")
        root2.geometry('600x300')
        if tes == 3:
            enemy = troll
        elif tes == 4:
            enemy = ork
        elif tes == 5:
            enemy = ticlop
        btn2 = Button(root2, text="напасть", command=fight)
        btn2.pack()
        btn3 = Button(root2, text="" + "  " + str(warrior.damage))
        btn3.pack()
        btn4 = Button(root2, text="ваше здоровье" + "  " + str(warrior.hp))
        btn4.pack()
        btn5 = Button(root2, text="здоровье врага" + "  " + str(enemy.hp))
        btn5.pack()


def fight():
    btn2.configure(text="ударить", command=fight)
    btn3.configure(text="" + "  " + str(warrior.damage))
    btn4.configure(text="ваше здоровье" + "  " + str(warrior.hp))
    btn5.configure(text="здоровье врага" + "  " + str(enemy.hp))
    warrior.hp -= enemy.damage
    enemy.hp -= warrior.damage
    died_text = "враг окачурился"
    if enemy.hp <= 0:
        btn5.configure(text=died_text)
        enemy.damage = 0
        btn2.configure(text="В меню", command=menu)
        warrior.coins += random.randrange(3, 8)
        lbl2.configure(text="Монеты" + "  " + str(warrior.coins))
        lbl2.grid(column=8, row=0)

    if warrior.hp <= 0:
        btn4.configure(text="опа, а ты окачурился")
        warrior.damage = 0
        btn2.configure(text="В меню", command=menu)
        warrior.coins = 0
        warrior.xp = 0


def menu():
    root2.destroy()
    lbl.destroy()
    btn.configure(text="в путь!", command=options3)


root.mainloop()
 

Vershitel_sudeb

Vershitel sudeb
Команда форума
Модератор
Мар 17, 2021
933
208
43
20
Москва
у тебя после смерти вызывается функция menu(), а в ней lbl.destroy(), который уничтожает lbl. После ты пытаешься записать текст в этот lbl (в функции options3) и получаешь ошибку, ведь lbl не существует
 
  • Мне нравится
Реакции: Ilya+

Форум IT Специалистов