简体   繁体   English

Glade / GTK3和单选按钮

[英]Glade/GTK3 and radio buttons

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. 使用本教程https://python-gtk-3-tutorial.readthedocs.io/en/latest/builder.html我创建了一个类似的Glade GUI,它具有退出按钮和两个单选按钮A和B。我很困惑通过单选按钮。

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. 如果我使用GtkButton或GtkToggleButton,则rba函数会触发两次,我假定它在按钮变为活动按钮和非活动按钮时都触发。 Is that right? 那正确吗?

I don't need anything complicated, either 我也不需要任何复杂的东西

  1. a function which triggers when a button is clicked, within which I can find out which one is now active, OR 一种单击按钮时触发的功能,在该功能中我可以发现哪个按钮现在处于活动状态,或者

  2. 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. 编辑:经过大量实验,我发现第二部分代码适用于信号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>

This is one of the ways to set up Python + Gtk properly with the basic idea you already have. 这是使用已有的基本思想正确设置Python + Gtk的方法之一。 It works for me. 这个对我有用。

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())

With the code below and Glade sending 'clicked' signals, it triggers both the 'switched on' and 'switched off' functions, but only once each and I can test which is on. 通过下面的代码,Glade发送“单击”信号,它会触发“打开”和“关闭”功能,但每个功能仅触发一次,我可以测试哪个功能打开。 This will work for me. 这对我有用。 I have changed some names to 'dummy' to remind me that these don't seem to have any function that I want. 我将某些名称更改为“虚拟”,以提醒我这些似乎没有我想要的功能。 Thanks gtknerd! 谢谢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()) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM