使用本教程https://python-gtk-3-tutorial.readthedocs.io/en/latest/builder.html我创建了一个类似的Glade GUI,它有一个退出按钮和两个单选按钮A和B.我对单选按钮很困惑。
如果我使用GtkButton或GtkToggleButton,则rba函数会触发两次,我认为它会在按钮变为活动按钮和变为非活动按钮时触发。是对的吗?
我不需要任何复杂的事情
单击按钮时触发的功能,我可以在其中找出哪一个现在处于活动状态,或者
每个按钮的功能,仅在单击其自己的按钮时触发。
我还需要能够切换哪个按钮处于活动状态,最好不要触发/它的功能。
我误解了一些必要的东西吗?
编辑:经过多次实验,我发现第二位代码适用于信号GtkToggleButton> toggled> rba。虽然我无法获得与其他按钮相似的任何内容!
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>
答案 0 :(得分:1)
这是使用您已有的基本想法正确设置Python + Gtk的方法之一。它对我有用。
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
import sys
class GUI:
def __init__ (self):
builder = Gtk.Builder()
builder.add_from_file('test.glade')
builder.connect_signals(self)
window = builder.get_object("window1")
RBA = builder.get_object('radiobuttonA')
RBA.connect('toggled', self.rba)
RBB = builder.get_object('radiobuttonB')
RBB.connect('toggled', self.rbb)
window.show_all()
def buttonQuit(self, menuitem): # quit with Quit button
Gtk.main_quit()
def on_window1_destroy(self, window): # close window with 0 or X
Gtk.main_quit()
def rba(self, radiobutton):
print('in rba')
print (radiobutton.get_active())
def rbb(self, radiobutton):
print('in rbb')
print (radiobutton.get_active())
def main():
app = GUI()
Gtk.main()
if __name__ == "__main__":
sys.exit(main())
答案 1 :(得分:0)
使用下面的代码和Glade发送&#39;点击&#39;信号,它触发&#39;开启&#39;和&#39;关闭&#39;功能,但每次只能一次,我可以测试哪个是开启的。这对我有用。我已将一些名字改为&#39; dummy&#39;提醒我,这些似乎没有任何我想要的功能。谢谢gtknerd!
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
import sys
class GUI:
def __init__ (self):
builder = Gtk.Builder()
builder.add_from_file('/home/chris/Debreate/test/gtknerd.glade')
builder.connect_signals(self)
window = builder.get_object("window1")
dummy1 = builder.get_object('radiobuttonA')
dummy2 = builder.get_object('radiobuttonB')
dummy3 = builder.get_object('radiobuttonC')
window.show_all()
def buttonQuit(self, menuitem): # quit with Quit button
Gtk.main_quit()
def on_window1_destroy(self, window): # close window with 0 or X
Gtk.main_quit()
def rba(self, radiobuttonA):
#print('in rba:', radiobuttonA.get_active(), radiobuttonA.get_label()) #2
if radiobuttonA.get_active(): print('in rba:', radiobuttonA.get_label())
def rbb(self, radiobuttonB):
#print('in rbb:', radiobuttonB.get_active(), radiobuttonB.get_label()) #2
if radiobuttonB.get_active(): print('in rbb:', radiobuttonB.get_label())
def rbc(self, radiobuttonC):
#print('in rbc:', radiobuttonC.get_active(), radiobuttonC.get_label()) #2
if radiobuttonC.get_active(): print('in rbc:', radiobuttonC.get_label())
def main():
app = GUI()
Gtk.main()
if __name__ == "__main__":
sys.exit(main())
&#13;