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

时间:2019-05-08 23:08:23

标签: python for-loop button tkinter command

如果不是目标按钮,我想添加一个功能来在按下按钮时销毁按钮(以及所有上/下按钮)。

我希望这可以清除它。

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()

2 个答案:

答案 0 :(得分:1)

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

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

下面是修改后的代码,显示了如何执行此操作。请注意,我还优化了其他一些内容,并使其总体上更加遵循PEP 8 - Style Guide for Python Code建议。

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()

答案 1 :(得分:0)

我在您的代码中添加了几行,以使Button(如果不是目标值)销毁。

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

  2. 我在两个条件(高和低)中添加了event.widget.destroy(),因此得到的目标Button并不是目标destroy()

    < / li>
  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()