首页 > 解决方案 > 如何从列表中删除/忘记/销毁使用 for 循环生成的一组单选按钮?

问题描述

我是一名教师,制作一个基本的迷你 Steam 客户端作为我的编程课的学习练习。我们正在使用 Python 和 tkinter。

该程序生成两组单选按钮。The first is the users "game library" where they can select a game to play(when selecting a radio button nothing happens other than a label informs the user the game is about to be launched). 第二组单选按钮让用户选择要“购买”的新游戏。两组单选按钮都是使用 for 循环和列表生成的。

问题:我想做的是拥有它,因此如果您选择要“购买”的游戏,它将被添加到 user_library 列表中,然后销毁/忘记原始游戏库单选按钮集,然后使用 for 循环重新生成单选按钮。这现在也应该为新添加的游戏创建一个按钮。

我尝试过的“忘记”代码仅隐藏/删除由 for 循环生成的最后一个 raido 按钮。

注意:单选按钮用于演示目的,我知道下拉菜单会更好。

我计划使用连接到购买按钮的方法来执行此操作。

  1. 该方法首先会在 user_library 列表中附加新的游戏名称。2.然后销毁/忘记程序首次启动时由for循环生成的用户库单选按钮。3. 使用另一个 for 循环重新创建用户库单选按钮。

当我运行程序时,我尝试过的“忘记”代码仅隐藏/删除由 for 循环生成的最后一个raidobutton。

下面的代码是缩写的,但应该显示我正在尝试做的事情。我对附加列表很满意,但不明白为什么只有最后一个单选按钮被遗忘了。

我对解决问题的更好方法持开放态度

libraryGames=["The Witcher 3: Wild Hunt GOTE", "Jurassic World: Evolution", "Red Dead Redemption 2","Mass Effect Trilogy","Subnautica",]

saleGames=["SteamPunk 2077", 29.99, "Fallout 3", 3.99, "Warcraft 4: About dam time", 69.99, "Lego: Terminator", 19.99, "Homework simulator", 14.99]

def __init__(self, parent):
     #list that created the library game buttons

     for games in range (0, len (libraryGames)):
            rb = Radiobutton(frame1, variable = self.library_game, bg = 
            "#000000",fg="#ffffff", value = games, text = 
            libraryGames[games],command = self.library_choice)
            rb.grid(row = libraryrow, column=0, columnspan = 2,padx=25, 
            sticky=W,)
            libraryrow+=1

     #list that created the sale game buttons

     for items in range (0, len (saleGames),2):
            rb2 = Radiobutton(frame2, variable = self.sale_game, bg = 
            "#000000",fg="#ffffff",value = items, text = 
            saleGames[items],command = self.sale_choice)
            rb2.grid(row = salerow, column=0, columnspan = 2,padx=25, 
            sticky=W,)
            salerow+=1

#method that removes the radio buttons generated by the first loop when a purchase button is clicked.

def purchase(self):
        rb.grid_forget()
        # I would then add the loop code to create the radio buttons again

标签: pythontkintergrid

解决方案


首先创建一个列表来保存单选按钮,然后grid_forget在需要时循环遍历列表。

import tkinter as tk

root = tk.Tk()

libraryGames=["The Witcher 3: Wild Hunt GOTE", "Jurassic World: Evolution", "Red Dead Redemption 2","Mass Effect Trilogy","Subnautica",]

class GUI:
    def __init__(self, parent):
        frame1 = tk.Frame(parent)
        frame1.pack()
        self.holder_list = []

        for num,game in enumerate(libraryGames):
            rb = tk.Radiobutton(frame1, bg="#000000",fg="#ffffff",
                                value=game, text=game,command= "",selectcolor="grey")
            rb.grid(row = num, column=0, columnspan = 2,padx=25,sticky=tk.W,)
            self.holder_list.append(rb)

        frame2 = tk.Frame(parent)
        frame2.pack()
        tk.Button(frame2,text="Purchased",command=self.purchase).pack()

    def purchase(self):
        print (self.holder_list)
        for widget in self.holder_list:
            widget.grid_forget()

GUI(root)
root.mainloop()

推荐阅读