Создание всплывающего окна с кнопками - PullRequest
1 голос
/ 22 сентября 2019

Я пытаюсь сделать небольшую игру, которая показывает вам окно с 8 кнопками, и вы выбираете одну, чтобы выиграть.Следующие 7 кнопок должны быть пропущены.Окно выскакивает, но кнопок нет.Есть идеи, что не так с кодом?

from tkinter import *
import random

t = Tk()
t.title("Select button")
t.geometry("300x350")

def insert_buttons():
     count_buttons=8
     global buttons
     buttons = []
     good = random.randint (0,count_buttons-1)
     for i in range (count_buttons):
          if  i == good:
               buttons.append(Button(t, text  = "Select", command=hit))
          else:
               buttons.append(Button(t, text = "Select", command=miss))
     for i in buttons:
          i.pack (fill=BOTH, expand=YES)

def hit():
     for i in buttons:
          i.destroy()
     global etykiet
     etykiet = label(t, text = "BRAWO! It's a hit!")
     etykiet.pack(fill=BOTH, expand=YES)
     t.after(5000,restart)

def miss():
     for i in buttons:
          i.destroy()
          global etykiet
          etykieta = label(t, text - "Try again")
          etykieta.pack(fill=BOTH, expand=YES)
          t.after(5000, restart)

def restart():
     etyket.destroy()
     inserty_buttons()

1 Ответ

0 голосов
/ 22 сентября 2019

Я исправил ваш код, и он должен работать.Было довольно много ошибок, не забудьте просмотреть ваш код и проверить документацию.Также я рекомендую использовать отступы 4 пробела

from tkinter import *
import random

t = Tk()
t.title("Select button")
t.geometry("300x350")


def insert_buttons():
     count_buttons = 8
     global buttons
     buttons = []
     good = random.randint(0, count_buttons - 1)
     for i in range(count_buttons):
          if i == good:
               buttons.append(Button(t, text="Select", command=hit))
          else:
               buttons.append(Button(t, text="Select", command=miss))
     for i in buttons:
          i.pack(fill=BOTH, expand=YES)


def hit():
    currentbuttons = globals()['buttons']
    globals()['buttons'] = []
    for i in currentbuttons:
          i.destroy()
          etykieta = Label(t, text="BRAVO! ITS A HIT")
          etykieta.pack(fill=BOTH, expand=YES)
          buttons.append(etykieta)
    t.after(2000, restart)


def miss():
    currentbuttons = globals()['buttons']
    globals()['buttons'] = []
    for i in currentbuttons:
          i.destroy()
          etykieta = Label(t, text="Try again")
          etykieta.pack(fill=BOTH, expand=YES)
          buttons.append(etykieta)
    t.after(2000, restart)


def restart():
     for button in globals()['buttons']:
        button.destroy()
     globals()['buttons'] = []
     insert_buttons()


insert_buttons()
t.mainloop()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...