Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用参数中的列表在GUI中创建单选按钮_Python_Oop_User Interface_Tkinter - Fatal编程技术网

Python 使用参数中的列表在GUI中创建单选按钮

Python 使用参数中的列表在GUI中创建单选按钮,python,oop,user-interface,tkinter,Python,Oop,User Interface,Tkinter,我使用了一个列表作为GUI窗口的参数,列表中的每个项目都应该作为GUI窗口中的单选按钮创建。目前,我使用for循环为每个单选按钮生成不同的值,使用另一个for循环生成不同的列。但是,我只能为列表中的最后一项创建单选按钮 这就是我目前拥有的: from tkinter import * from tkinter.scrolledtext import * class ExpenditureGUI: def __init__(self, listOfCategories): self._

我使用了一个列表作为GUI窗口的参数,列表中的每个项目都应该作为GUI窗口中的单选按钮创建。目前,我使用for循环为每个单选按钮生成不同的值,使用另一个for循环生成不同的列。但是,我只能为列表中的最后一项创建单选按钮

这就是我目前拥有的:

from tkinter import *
from tkinter.scrolledtext import *

class ExpenditureGUI:

def __init__(self, listOfCategories):
    self._listOfCategories = listOfCategories
    self._tk = Tk()
    self._tk.title('Expenditure Tracker')     
    self._tk.geometry('500x200')        
    self._tk.resizable(False,False)
    self.initWidgits()
    self._tk.mainloop()


@property    
def listOfCategories(self):
    return self._listOfCategories
@listOfCategories.setter
def listOfCategories(self, newValue):
    self._listOfCategories = newValue

def initWidgits(self):
    #flow layout
    topF = Frame(self._tk)
    self._lblAmount = Label(topF, text = 'Amount: ')      
    self._txtAmount = Entry(topF, width=30)


    rbtnF = Frame(topF)
    self._rbtnVar = IntVar()  
    self._rbtnVar.set(0)  
    self._lblExpenditure = Label(topF, text = 'Type of Expenditure: ')

    n = 0
    for type in self._listOfCategories:
        self._rbtntype = Radiobutton(rbtnF, text = f'{type}', value = n, variable = self._rbtnVar)


    self._lblExpenditure.grid(row = 0, column = 0, sticky = E)

    n = 0
    for type in self._listOfCategories:
        self._rbtntype.grid(row = 0, column = n)

我不是GUI库的专家,但这看起来很可疑:

for type in self._listOfCategories:
        self._rbtntype = Radiobutton(rbtnF, text = f'{type}', value = n, variable = self._rbtnVar)
                                            ^^^^^^^^^^^^^^^^
循环结束后,
self.\u rbtntype
变量将包含仅引用类别列表中最后一个元素的文本(即循环变量
type

您可能希望在代码示例的最后一个循环中构造一个新的
Radiobutton
。也许像这样的东西会有用。它可能需要在每个循环迭代中更新
n

for type in self._listOfCategories:
    rbtntype = Radiobutton(rbtnF, text = f'{type}', value = n, variable = self._rbtnVar)
    rbtntype.grid(row = 0, column = n)

我不是GUI库的专家,但这看起来很可疑:

for type in self._listOfCategories:
        self._rbtntype = Radiobutton(rbtnF, text = f'{type}', value = n, variable = self._rbtnVar)
                                            ^^^^^^^^^^^^^^^^
循环结束后,
self.\u rbtntype
变量将包含仅引用类别列表中最后一个元素的文本(即循环变量
type

您可能希望在代码示例的最后一个循环中构造一个新的
Radiobutton
。也许像这样的东西会有用。它可能需要在每个循环迭代中更新
n

for type in self._listOfCategories:
    rbtntype = Radiobutton(rbtnF, text = f'{type}', value = n, variable = self._rbtnVar)
    rbtntype.grid(row = 0, column = n)

断然的!我意识到第一个循环没有创建任何单选按钮,谢谢!断然的!我意识到第一个循环没有创建任何单选按钮,谢谢!