2

在 wxPython 中,如果我创建一个单选按钮列表并最初放置该列表,以后是否可以更改该列表中的内容?

例如,我有一个面板,它最初使用 boxSizer 来放置小部件。其中一个小部件是单选按钮列表(我也尝试过普通的单选框)。我想根据另一个类的变量动态更改列表。

但是,一旦将列表放入 sizer 中,它就会被有效地“锁定”;我不能只修改列表并显示更改。如果我尝试将列表重新添加到 sizer,它只会放在面板的左上角。

我确信我可以隐藏原始列表并手动将新列表放在相同的位置,但这感觉就像一个杂物。我敢肯定,我让这件事变得比现在更难。我可能为此使用了错误的小部件,更不用说错误的方法,但我将其构建为一种学习体验。

    class Job(wiz.WizardPageSimple):
    """Character's job class."""

    def __init__(self, parent, title, attribs):
        wiz.WizardPageSimple.__init__(self, parent)
        self.next = self.prev = None
        self.sizer = makePageTitle(self, title)
        self.charAttribs = attribs

#---Create widgets
        self.Job_list = ["Aircraft Mechanic", "Vehicle Mechanic", "Electronics Specialist"]

        box1_title = wx.StaticBox( self, -1, "" )
        box1 = wx.StaticBoxSizer( box1_title, wx.VERTICAL )
        grid1 = wx.BoxSizer(wx.VERTICAL)
        for item in self.Job_list:
            radio = wx.RadioButton(self, -1, item)
            grid1.Add(radio)

##Debugging
        self.btn = wx.Button(self, -1, "click")
        self.Bind(wx.EVT_BUTTON, self.eligibleJob, self.btn)

#---Place widgets
        self.sizer.Add(self.Job_intro)
        self.sizer.Add(self.btn)
        box1.Add(grid1)
        self.sizer.Add(box1)        

    def eligibleJob(self, event):
        """Determine which Jobs a character is eligible for."""

        if self.charAttribs.intelligence >= 12:
            skillList = ["Analyst", "Interrogator", "Fire Specialist", "Aircraft Pilot"]
            for skill in skillList:
                self.Job_list.append(skill)
            print self.Job_list ##Debugging
        #return self.Job_list
4

3 回答 3

1

要使新的列表元素出现在正确的位置,您必须在添加新元素后重新布局网格。例如,要添加一些新项目,您可以调用:

def addNewSkills(self, newSkillList):
    '''newSkillList is a list of skill names you want to add'''
    for skillName in newSkillList:
        newRadioButton = wx.RadioButton(self, -1, skillName)
        self.grid1.Add(newRadioButton) # appears in top-left corner of the panel
    self.Layout() # all newly added radio buttons appear where they should be
    self.Fit() # if you need to resize the panel to fit new items, this will help

self.grid1你保持所有单选按钮的大小在哪里。

于 2008-09-26T12:06:40.560 回答
0

两种可能的解决方案

  1. 每次您必须进行更改时,使用单选小部件重建 sizer
  2. 按住列表中的单选按钮小部件,并在每次必须更改其标签时调用 SetLabel。
于 2008-09-26T09:34:03.090 回答
0

我能够通过使用 DzinX 提供的信息修复它,并进行一些修改。

It appears that posting the radio buttons box first "locked in" the box to the sizer. If I tried to add a new box, I would get an error message stating that I was trying to add the widget to the same sizer twice.

By simply removing the radio buttons initially and having the user click a button to call a method, I could simply add a the list of radio buttons without a problem.

Additionally, by having the user click a button, I did not run into errors of "class Foo has no attribute 'bar'". Apparently, when the wizard initalizes, the attributes aren't available to the rest of the wizard pages. I had thought the wizard pages were dynamically created with each click of "Next" but they are all created at the same time.

于 2008-09-28T09:47:15.387 回答