PyGTK Radio Button

Ok, I will predetermine this by being GTK and Python newb, but I couldn't find the information I needed. Basically, I have a list of radio buttons, and based on which it is being checked, I need to connect the button to another function. I tried to create all my radio buttons and then create a disgusting if / else block to check sget_active()

on each button. The problem is that the same button returns true every time. Any ideas?

Here's the code used:

    #Radio Buttons Center
    self.updatePostRadioVBox = gtk.VBox(False, 0)
    self.updatePageRadio = gtk.RadioButton(None, "Updating Page")
    self.updatePostRadio = gtk.RadioButton(self.updatePageRadio, "Updating Blog Post")
    self.pageRadio = gtk.RadioButton(self.updatePageRadio, "New Page")
    self.blogRadio = gtk.RadioButton(self.updatePageRadio, "New Blog Post")
    self.addSpaceRadio = gtk.RadioButton(self.updatePageRadio, "Add New Space")
    self.removePageRadio = gtk.RadioButton(self.updatePageRadio, "Remove Page")
    self.removePostRadio = gtk.RadioButton(self.updatePageRadio, "Remove Blog Post")
    self.removeSpaceRadio = gtk.RadioButton(self.updatePageRadio, "Remove Space")


    #Now the buttons to direct us from here
    self.returnMainMenuButton = gtk.Button(" Main Menu ")
    self.returnMainMenuButton.connect("clicked", self.transToMain)
    self.contentManageHBoxBottom.pack_start(self.returnMainMenuButton, False, False, 30)
    self.contentProceedButton = gtk.Button("    Proceed    ")
    self.contentManageHBoxBottom.pack_end(self.contentProceedButton, False, False, 30)

    if self.updatePageRadio.get_active():
        self.contentProceedButton.connect("clicked", self.updatePage)

    elif self.updatePostRadio.get_active():
        self.contentProceedButton.connect("clicked", self.updatePost)

    elif self.pageRadio.get_active():
        self.contentProceedButton.connect("clicked", self.newPage)

    elif self.blogRadio.get_active():
        self.contentProceedButton.connect("clicked", self.newBlogPost)

    elif self.addSpaceRadio.get_active():
        self.contentProceedButton.connect("clicked", self.newSpace)

    elif self.removePageRadio.get_active():
        self.contentProceedButton.connect("clicked", self.removePage)

    elif self.removePostRadio.get_active():
        self.contentProceedButton.connect("clicked", self.removeBlogPost)

    elif self.removeSpaceRadio.get_active():
        self.contentProceedButton.connect("clicked", self.removeSpace)

      

+1


source to share


2 answers


Edit: (since you posted some code) just use:

active = [r for r in self.updatePageRadio.get_group() if r.get_active()][0]

      

and use this to search in the function dict and call it:

my_actions[active]()

      


Edit: I completely forgot to mention that this is not very useful for RadioButtons, a regular gtk.Button would be much better in my opinion.




Your answer is to use the "RadioButton" group system. This is explained in this document , but here's a small practical example.

First, the group is actually just the RadioButton itself, which is used to collect a number of other RadioButtons. You supply the group as the first argument to the constructor.

r1 = gtk.RadioButton(None, label='Cat') # this has no group, it is the first
r2 = gtk.RadioButton(r1, label='Mouse') # use the first radio
                                        # button as the group argument
r3 = gtk.RadioButton(r1, label='Dog')   # again use r1

      

All switches will now be in sync. And the matter of reading them is as simple as:

active_radios = [r for r in r1.get_group() if r.get_active()]

      

+7


source


First, I'm assuming there is a typo and you are actually calling get_active () in your code and not set_active ()? Apart from that, without seeing the code, I can point you to the pygtk manual about radio buttons



0


source







All Articles