如果按钮是由 for 循环制作的,如何在单击按钮时删除按钮?



我想添加一个函数,如果按钮不是目标按钮,则在按下按钮(以及所有更高/更低的按钮)时将其销毁。

我希望这能澄清一点。

import tkinter as tk
from tkinter import ttk
import random
window = tk.Tk()
window.title('The Game')
window.geometry('1000x850')
target = random.randint(1,10)
words = []
for j in range(1, 11):
unit = "box" + str(j)
words.append(unit)
a=0
lives = 3
for i in words:
a = a + 1
def btn_press(event):
guess = event.widget['text']
global lives
lives -= 1
lives_rem.config(text = 'Lives remaining: ' + str(lives))
if guess == target:
print('you win')
window.destroy()
elif lives == 0:
print('you lose')
window.destroy()
elif guess > target:
#in this case, the button pressed and all of the higher ones should be destroyed
print('too high')
elif guess < target:
#in this case, the button pressed and all of the lower ones should be destroyed
print('too low')
i = tk.Button(window, text = a)
i.config(height = '3', width = '6')
i.bind('<Button-1>', btn_press)
i.place(x = -50 + a * 70, y = 25)
lives_rem = tk.Label(window, text = "Lives remaining: " + str(lives), fg = 'red')
lives_rem.place(x = 800, y = 50)
window.mainloop()

自从我第一次发表评论以来,您已经更改了问题,因此现在您需要跟踪创建的所有Button,因为您现在想要"销毁"的不仅仅是单击的那个。在下面的代码中,它们存储在名为buttons的新全局list变量中。

使用place_forget()方法可以使与place()几何管理器一起显示的单个微件消失。将其与新的buttons列表相结合,还可以在'<Button-1>'事件回调函数中影响其他列表的可见性。

下面是您的代码,其中包含显示如何执行此操作的修改。请注意,我还优化了其他一些内容,并使其总体上更严格地遵循 PEP 8 - Python 代码样式指南建议。

import tkinter as tk
import random
window = tk.Tk()
window.title('The Game')
window.geometry('1000x850')
target = random.randint(1,10)
print('target:', target)
words = ["box" + str(j) for j in range(1, 11)]
a = 0
lives = 3
buttons = []  # Remember all Buttons.
for _ in words:
a += 1
def btn_press(event):
global lives
guess = event.widget['text']
lives -= 1
lives_rem.config(text='Lives remaining: ' + str(lives))
if guess == target:
print('you win')
window.destroy()
elif lives == 0:
print('you lose')
window.destroy()
elif guess > target:
# In this case, the button pressed and all of the higher ones
# should be destroyed.
event.widget.place_forget()
print('too high')
for btn in buttons:
if btn['text'] > guess:
btn.place_forget()
elif guess < target:
# In this case, the button pressed and all of the lower ones should
# be destroyed.
event.widget.place_forget()  # Added MRM
print('too low')
for btn in buttons:
if btn['text'] < guess:
btn.place_forget()

btn = tk.Button(window, text=a)
btn.config(height=3, width=6)
btn.bind('<Button-1>', btn_press)
btn.place(x=a*70 - 50, y=25)
buttons.append(btn)
lives_rem = tk.Label(window, text="Lives remaining: " + str(lives), fg='red')
lives_rem.place(x=800, y=50)
window.mainloop()

我在代码中添加了几行,以使Button在不是目标值时销毁。

  1. 我不明白为什么你在 for 块中有函数btn_press()。它创建了大约 10 个btn_press()函数。我猜你每个按钮都有自己的功能,但相信我一个就足够了。所以我把函数放在for循环之外

  2. 我在两个条件(高和低)中添加了event.widget.destroy(),所以它不是目标Buttondestroy()

  3. 此外,您的方法不好,您可以大量改进代码。

这是您更新的代码。

from tkinter import ttk
import tkinter as tk
import random
window = tk.Tk()
window.title('The Game')
window.geometry('1000x850')
target = random.randint(1,10)
words = []
for j in range(1, 11):
unit = "box" + str(j)
words.append(unit)
a=0
lives = 3

def btn_press(event):
guess = event.widget['text']
global lives
lives -= 1
lives_rem.config(text = 'Lives remaining: ' + str(lives))
if guess == target:
print('you win')
Score['text'] = 'Score: You Win' 
window.destroy()
elif lives == 0:
print('you lose')
Score['text'] = 'Score: You Lose' 
window.destroy()
elif guess > target:
#in this case, the button pressed and all of the higher ones should be destroyed
Score['text'] = 'Score: Too High' 
print('too high')
event.widget.destroy()
elif guess < target:
#in this case, the button pressed and all of the lower ones should be destroyed
Score['text'] = 'Score: Too Low' 
print('too low')
event.widget.destroy()
for i in words:
a = a + 1
i = tk.Button(window, text = a)
i.config(height = '3', width = '6')
i.bind('<Button-1>', btn_press)
i.place(x = -50 + a * 70, y = 25)
lives_rem = tk.Label(window, text = "Lives remaining: " + str(lives), fg = 'red')
lives_rem.place(x = 800, y = 50)
Score = tk.Label(window, text = "Score: " )
Score.place(x = 400, y = 100 )
window.mainloop()

最新更新