Using this tutorial https://python-gtk-3-tutorial.readthedocs.io/en/latest/builder.html I have created a similar Glade GUI which has a quit button and two radio buttons A and B. I am quite confused by radio buttons.
If I use GtkButton or GtkToggleButton the rba function triggers twice, I assume it triggers both when the button becomes the active one and when it becomes the inactive one. Is that right?
I don't need anything complicated, either
a function which triggers when a button is clicked, within which I can find out which one is now active, OR
a function for each button which triggers only when its own button is clicked.
I also need to be able to switch which button is active, preferably without triggering the/its function.
Have I misunderstood something essential?
EDIT: After much experimentation, I have found that second bit of code works for a signal GtkToggleButton>toggled>rba. I can't get anything similar to work for the other button though!
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class Handler:
def buttonQuit(self, menuitem): # quit with Quit button
Gtk.main_quit()
def on_window1_destroy(self, object): # close window with 0 or X
Gtk.main_quit()
def rba(self, menuitem):
print('A')
builder = Gtk.Builder()
builder.add_from_file('test.glade')
builder.connect_signals(Handler())
window = builder.get_object("window1")
window.show_all()
Gtk.main()
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class Handler:
def buttonQuit(self, menuitem): # quit with Quit button
Gtk.main_quit()
def on_window1_destroy(self, object): # close window with 0 or X
Gtk.main_quit()
def rba(self, menuitem):
print('in rba')
print (RBA.get_active())
builder = Gtk.Builder()
builder.add_from_file('test.glade')
builder.connect_signals(Handler())
window = builder.get_object("window1")
RBA = builder.get_object('radiobuttonA')
window.show_all()
Gtk.main()
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
<requires lib="gtk+" version="3.12"/>
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<property name="window_position">center</property>
<property name="gravity">center</property>
<property name="has_resize_grip">True</property>
<signal name="destroy" handler="on_window1_destroy" swapped="no"/>
<child>
<object class="GtkBox" id="box1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkButtonBox" id="buttonbox1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">center</property>
<property name="valign">center</property>
<property name="layout_style">start</property>
<child>
<object class="GtkRadioButton" id="radiobuttonA">
<property name="label" translatable="yes">A</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="halign">start</property>
<property name="valign">center</property>
<property name="xalign">0</property>
<property name="active">True</property>
<property name="draw_indicator">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkRadioButton" id="radiobuttonB">
<property name="label" translatable="yes">B</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="halign">start</property>
<property name="valign">center</property>
<property name="xalign">0</property>
<property name="draw_indicator">True</property>
<property name="group">radiobuttonA</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="buttonQuit">
<property name="label" translatable="yes">Quit</property>
<property name="width_request">100</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="halign">center</property>
<property name="valign">center</property>
<property name="xalign">0.56000000238418579</property>
<property name="yalign">0.49000000953674316</property>
<signal name="clicked" handler="buttonQuit" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
toggled
signal of both radiobuttons? You then have to figure out which radiobutton is active with something likeradiobutton.get_active()
. – theGtknerd