PyGTK: Radiobuttons are still displayed after removal
- by canavanin
Hi everyone!
I am using PyGTK and the gtk.assistant. On one page I would like to display two radiobuttons in case the user selected a certain option on a previous page. The labels of the buttons - and whether the buttons are to be present at all - are to depend entirely on that earlier selection. Furthermore, if the user goes back and changes that selection, the page containing the radiobuttons is to be updated accordingly.
I have got as far as having the radiobuttons displayed when necessary, and with the correct labels. The trouble is that if I go back and change the determining selection, or if I move one page further than the 'radiobutton page' and then move back, the buttons are not only not removed (in case that would have been required), their number has also doubled.
To show you what I'm doing, here's part of my code (I've left out bits that do unrelated things, that's why the function name doesn't fit). The function is called when the "prepare" signal is emitted prior to construction of the 'radiobutten page'.
def make_class_skills_treestore(self):
print self.trained_by_default_hbox.get_children() # PRINT 1
for child in self.trained_by_default_hbox.get_children():
if type(child) == gtk.RadioButton:
self.trained_by_default_hbox.remove(child)
#child.destroy() # <-- removed the labels, but not the buttons
print self.trained_by_default_hbox.get_children() # PRINT 2
class_skills = self.data.data['classes'][selected_class].class_skills.values()
default_trained_count = (class_skills.count([True, True]) , class_skills.count([True, False]))
num_default_trained_skills = default_trained_count[1] / 2 # you have to pick one of a pair --> don't
# count each as trained by default
for i in range(default_trained_count[0]): # those are trained by default --> no choice
num_default_trained_skills +=1
selected_class = self.get_classes_key_from_class_selection()
if default_trained_count[1]:
for skill in self.data.data['classes'][selected_class].class_skills.keys():
if self.data.data['classes'][selected_class].class_skills[skill] == [ True, False ] and not self.default_radio:
self.default_radio.append(gtk.RadioButton(group=None, label=skill))
elif self.data.data['classes'][selected_class].class_skills[skill] == [ True, False ] and self.default_radio:
self.default_radio.append(gtk.RadioButton(group=self.default_radio[0], label=skill))
if self.default_radio:
for radio in self.default_radio:
self.trained_by_default_hbox.add(radio)
self.trained_by_default_hbox.show_all()
self.trained_by_default_hbox and self.trained_by_default_label, as well as self.default_radio stem from the above function's class.
I have two print statements (PRINT 1 and PRINT 2) in there for debugging. Here's what they give me:
PRINT 1:
[<gtk.Label object at 0x8fc4c84 (GtkLabel at 0x90a2f20)>, <gtk.RadioButton object at 0x8fc4d4c (GtkRadioButton at 0x90e4018)>, <gtk.RadioButton object at 0x8fc4cac (GtkRadioButton at 0x90ceec0)>]
PRINT 2:
[<gtk.Label object at 0x8fc4c84 (GtkLabel at 0x90a2f20)>]
So the buttons have indeed been removed, yet they still show up on the page.
I know the code requires some refactoring, but first I'd like to get it to work at all... If someone could help me out that would be great!
Thanks a lot in advance for your replies - any kind of help is highly appreciated.