Python 使用按钮创建弹出窗口

Python 使用按钮创建弹出窗口,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我想做一个小游戏,显示一个有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 = []

我想做一个小游戏,显示一个有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()

我把你的代码修好了,应该可以用了。有相当多的错误,记得检查代码和文档。我还建议使用4个空格缩进

从tkinter导入*
随机输入
t=Tk()
t、 标题(“选择按钮”)
t、 几何图形(“300x350”)
def插入按钮():
计数按钮=8
全局按钮
按钮=[]
good=random.randint(0,计数按钮-1)
对于范围内的i(计数按钮):
如果i==好:
按钮。追加(按钮(t,text=“Select”,command=hit))
其他:
按钮。追加(按钮(t,text=“Select”,command=miss))
对于i输入按钮:
i、 打包(填充=两者,展开=是)
def hit():
currentbuttons=globals()['buttons']
globals()['buttons']=[]
对于当前按钮中的i:
i、 销毁
etykieta=Label(t,text=“好极了!很成功”)
etykieta.pack(填充=两者,扩展=是)
按钮。附加(etykieta)
t、 之后(2000年,重新启动)
def miss():
currentbuttons=globals()['buttons']
globals()['buttons']=[]
对于当前按钮中的i:
i、 销毁
etykieta=标签(t,text=“重试”)
etykieta.pack(填充=两者,扩展=是)
按钮。附加(etykieta)
t、 之后(2000年,重新启动)
def restart():
对于globals()中的按钮['buttons']:
button.destroy()
globals()['buttons']=[]
插入按钮()
插入按钮()
t、 mainloop()

我修复了您的代码,它应该可以正常工作。有相当多的错误,记得检查代码和文档。我还建议使用4个空格缩进

从tkinter导入*
随机输入
t=Tk()
t、 标题(“选择按钮”)
t、 几何图形(“300x350”)
def插入按钮():
计数按钮=8
全局按钮
按钮=[]
good=random.randint(0,计数按钮-1)
对于范围内的i(计数按钮):
如果i==好:
按钮。追加(按钮(t,text=“Select”,command=hit))
其他:
按钮。追加(按钮(t,text=“Select”,command=miss))
对于i输入按钮:
i、 打包(填充=两者,展开=是)
def hit():
currentbuttons=globals()['buttons']
globals()['buttons']=[]
对于当前按钮中的i:
i、 销毁
etykieta=Label(t,text=“好极了!很成功”)
etykieta.pack(填充=两者,扩展=是)
按钮。附加(etykieta)
t、 之后(2000年,重新启动)
def miss():
currentbuttons=globals()['buttons']
globals()['buttons']=[]
对于当前按钮中的i:
i、 销毁
etykieta=标签(t,text=“重试”)
etykieta.pack(填充=两者,扩展=是)
按钮。附加(etykieta)
t、 之后(2000年,重新启动)
def restart():
对于globals()中的按钮['buttons']:
button.destroy()
globals()['buttons']=[]
插入按钮()
插入按钮()
t、 mainloop()

8个标签中的“重试”使用此选项就像是originaly@Barb一样,我刚刚让它工作了你将在8个标签中的“重试”使用此选项就像originaly@Barb一样,我刚刚让它工作了