Флажок с CSS в GLade Python

Все, кого я пытаюсь изменить цвета флажков, когда они нажаты, потому что я делаю работу, которая требует их использования. Я использую их без атрибута "Индикатор чертежа", поэтому мне нужно, чтобы при нажатии они имели другой цвет, потому что он не очень хорошо выглядит и не очень привлекателен. Есть какой-либо способ сделать это? Версия моей поляны 3.18.3. Я использую модель Pi3 B

Это мой код:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import GObject as gobject
from gi.repository import Gtk, Gdk
import gtk


#Checkbuttons


def on_checkbutton_toggled(button, name):
    if (button.get_active()):
        state = "Active"

    else:
        state = "Inactive"
    print("port " + name + " toggled, state is " +state)


def start(button):
    print ("Button start clicked")

def save(button):
    print ("Button save clicked")

def clear(button):
    print ("Button clear clicked")

def select_all(button):
    print ("Button all clicked")

def details(button):
    print ("Button details clicked")

def click_for_help(button):
    print ("Button help clicked")

def gtk_style():
    css= b"""
.button:checked {
    color:white;
    background:red;
}
"""
    style_provider = Gtk.CssProvider()
    style_provider.load_from_data(css)

    Gtk.StyleContext.add_provider_for_screen(
        Gdk.Screen.get_default(),
        style_provider,
        Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
    )

    gtk_style()

# GUI (Graphical User Interface)
builder = Gtk.Builder()                                                                                                                                  
builder.add_from_file("/home/pi/Desktop/Wi-Green_Project/schedule-0.4.3/Wi-GreenSPM.glade")                                                                                                         
handlers = {
        "action_start": start,
        "action_save": save,
        "action_clear": clear,
        "action_all": select_all,
        "action_details": details,
        "action_help":click_for_help


    }

builder.connect_signals(handlers)
window = builder.get_object("window_WG")


checkb1 = builder.get_object("port1")
checkb2 = builder.get_object("port2")
checkb3 = builder.get_object("port3")
checkb4 = builder.get_object("port4")
checkb5 = builder.get_object("port5")
checkb6 = builder.get_object("port6")
checkb7 = builder.get_object("port7")
checkb8 = builder.get_object("port8")
checkb9 = builder.get_object("port9")
checkb10 = builder.get_object("port10")
checkb11 = builder.get_object("port11")
checkb12 = builder.get_object("port12")
checkb13 = builder.get_object("port13")
checkb14 = builder.get_object("port14")
checkb15 = builder.get_object("port15")
checkb16 = builder.get_object("port16")
checkb17 = builder.get_object("port17")
checkb18 = builder.get_object("port18")
checkb19 = builder.get_object("port19")
checkb20 = builder.get_object("port20")
checkb21 = builder.get_object("port21")
checkb22 = builder.get_object("port22")
checkb23 = builder.get_object("port23")
checkb24 = builder.get_object("port24")

checkb1.connect ("toggled", on_checkbutton_toggled, "1")
checkb2.connect ("toggled", on_checkbutton_toggled, "2")
checkb3.connect ("toggled", on_checkbutton_toggled, "3")
checkb4.connect ("toggled", on_checkbutton_toggled, "4")
checkb5.connect ("toggled", on_checkbutton_toggled, "5")
checkb6.connect ("toggled", on_checkbutton_toggled, "6")
checkb7.connect ("toggled", on_checkbutton_toggled, "7")
checkb8.connect ("toggled", on_checkbutton_toggled, "8")
checkb9.connect ("toggled", on_checkbutton_toggled, "9")
checkb10.connect ("toggled", on_checkbutton_toggled, "10")
checkb11.connect ("toggled", on_checkbutton_toggled, "11")
checkb12.connect ("toggled", on_checkbutton_toggled, "12")
checkb13.connect ("toggled", on_checkbutton_toggled, "13")
checkb14.connect ("toggled", on_checkbutton_toggled, "14")
checkb15.connect ("toggled", on_checkbutton_toggled, "15")
checkb16.connect ("toggled", on_checkbutton_toggled, "16")
checkb17.connect ("toggled", on_checkbutton_toggled, "17")
checkb18.connect ("toggled", on_checkbutton_toggled, "18")
checkb19.connect ("toggled", on_checkbutton_toggled, "19")
checkb20.connect ("toggled", on_checkbutton_toggled, "20")
checkb21.connect ("toggled", on_checkbutton_toggled, "21")
checkb22.connect ("toggled", on_checkbutton_toggled, "22")
checkb23.connect ("toggled", on_checkbutton_toggled, "23")
checkb24.connect ("toggled", on_checkbutton_toggled, "24")
#---------------------------------------------------------------------------------------

window.connect("destroy", Gtk.main_quit)

window.show_all()

Gtk.main()

1 ответ

Решение

Поскольку Gtk.CheckButton устанавливается без индикатора отрисовки, то он в основном становится тумблером и классом CSS, на Gtk+ 3.18 .button,

Определить провайдера для этого класса и добавить его на экран довольно просто.

Во-первых, соответствующий код CSS:

.button:checked {
    color: white;
    background: red;
}

В этом примере кнопка, если она отмечена, будет иметь красный фон с белым текстом.

Рабочий пример будет:

#!/usr/bin/python3
from gi.repository import Gtk, Gdk
import sys


class MyWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Hello World")
        self.set_name('MyWindow')
        self.set_default_size(600, 300)

        self.box = Gtk.HBox()
        self.box.set_halign(Gtk.Align.CENTER)
        self.box.set_valign(Gtk.Align.CENTER)
        self.add(self.box)

        self.button1 = Gtk.CheckButton(label="1")
        self.button1.set_mode (False);
        self.button1.connect("clicked", self.on_button1_clicked)
        self.box.pack_start(self.button1, True, True, 0)

        self.button2 = Gtk.CheckButton(label="2")
        self.button2.set_mode (False);
        self.button2.connect("clicked", self.on_button2_clicked)
        self.box.pack_start(self.button2, True, True, 0)

    def on_button1_clicked(self, widget):
        print("Button 1")

    def on_button2_clicked(self, widget):
        print("Button 2")


def main(argv):

    def gtk_style():
        css = b"""
.button:checked {
    color: white;
    background: red;
}
        """
        style_provider = Gtk.CssProvider()
        style_provider.load_from_data(css)

        Gtk.StyleContext.add_provider_for_screen(
            Gdk.Screen.get_default(),
            style_provider,
            Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
        )

    gtk_style()
    win = MyWindow()
    win.connect("delete-event", Gtk.main_quit)
    win.show_all()
    Gtk.main()

if __name__ == "__main__":
    main(sys.argv)

Результат примерно такой:

Checkbutton

Это будет применять одинаковое поведение ко всем кнопкам проверки, поэтому, если идея состоит в том, чтобы использовать это решение только для этих пронумерованных кнопок, но при этом необходимо использовать другие кнопки проверки (как кнопки переключения) в приложении, то вам необходимо добавить / удалить класс стиля или добавить собственный стиль. класс и т. д.

РЕДАКТИРОВАТЬ: Решение кода вопроса не работает.

Проблема заключается в том, что gtk_style () не вызывается. Вызов находится внутри собственного определения:

def gtk_style():
   ...code...
   gtk_style() # <----- Here is the problem.

Отступы Python актуальны. Так оно и есть сейчас, оно становится рекурсивным и не вызывается. Решение состоит в том, чтобы удалить эту строку и переместить ее в код ниже, следуя, например, строке window.connect.

Пустой файл поляны (myui.glade):

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.19.0 -->
<interface>
  <requires lib="gtk+" version="3.16"/>
  <object class="GtkWindow" id="window1">
    <property name="can_focus">False</property>
    <child>
      <object class="GtkGrid" id="grid1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <child>
          <object class="GtkCheckButton" id="port1">
            <property name="label" translatable="yes">Port</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">False</property>
            <property name="xalign">0</property>
            <property name="draw_indicator">False</property>
          </object>
          <packing>
            <property name="left_attach">0</property>
            <property name="top_attach">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkCheckButton" id="port2">
            <property name="label" translatable="yes">Port</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">False</property>
            <property name="xalign">0.5</property>
            <property name="draw_indicator">False</property>
          </object>
          <packing>
            <property name="left_attach">1</property>
            <property name="top_attach">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkCheckButton" id="port3">
            <property name="label" translatable="yes">Port</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">False</property>
            <property name="xalign">0.5</property>
            <property name="draw_indicator">False</property>
          </object>
          <packing>
            <property name="left_attach">2</property>
            <property name="top_attach">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkCheckButton" id="port4">
            <property name="label" translatable="yes">Port</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">False</property>
            <property name="xalign">0.5</property>
            <property name="draw_indicator">False</property>
          </object>
          <packing>
            <property name="left_attach">3</property>
            <property name="top_attach">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkCheckButton" id="port5">
            <property name="label" translatable="yes">Port</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">False</property>
            <property name="xalign">0.5</property>
            <property name="draw_indicator">False</property>
          </object>
          <packing>
            <property name="left_attach">4</property>
            <property name="top_attach">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkCheckButton" id="port6">
            <property name="label" translatable="yes">Port</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">False</property>
            <property name="xalign">0.5</property>
            <property name="draw_indicator">False</property>
          </object>
          <packing>
            <property name="left_attach">5</property>
            <property name="top_attach">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkCheckButton" id="port7">
            <property name="label" translatable="yes">Port</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">False</property>
            <property name="xalign">0.5</property>
            <property name="draw_indicator">False</property>
          </object>
          <packing>
            <property name="left_attach">6</property>
            <property name="top_attach">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkCheckButton" id="port8">
            <property name="label" translatable="yes">Port</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">False</property>
            <property name="xalign">0.5</property>
            <property name="draw_indicator">False</property>
          </object>
          <packing>
            <property name="left_attach">7</property>
            <property name="top_attach">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkCheckButton" id="port9">
            <property name="label" translatable="yes">Port</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">False</property>
            <property name="xalign">0.5</property>
            <property name="draw_indicator">False</property>
          </object>
          <packing>
            <property name="left_attach">8</property>
            <property name="top_attach">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkCheckButton" id="port10">
            <property name="label" translatable="yes">Port</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">False</property>
            <property name="xalign">0.5</property>
            <property name="draw_indicator">False</property>
          </object>
          <packing>
            <property name="left_attach">9</property>
            <property name="top_attach">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkCheckButton" id="port11">
            <property name="label" translatable="yes">Port</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">False</property>
            <property name="xalign">0.5</property>
            <property name="draw_indicator">False</property>
          </object>
          <packing>
            <property name="left_attach">10</property>
            <property name="top_attach">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkCheckButton" id="port12">
            <property name="label" translatable="yes">Port</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">False</property>
            <property name="xalign">0.5</property>
            <property name="draw_indicator">False</property>
          </object>
          <packing>
            <property name="left_attach">11</property>
            <property name="top_attach">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkCheckButton" id="port13">
            <property name="label" translatable="yes">Port</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">False</property>
            <property name="xalign">0.5</property>
            <property name="draw_indicator">False</property>
          </object>
          <packing>
            <property name="left_attach">0</property>
            <property name="top_attach">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkCheckButton" id="port14">
            <property name="label" translatable="yes">Port</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">False</property>
            <property name="xalign">0.5</property>
            <property name="draw_indicator">False</property>
          </object>
          <packing>
            <property name="left_attach">1</property>
            <property name="top_attach">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkCheckButton" id="port15">
            <property name="label" translatable="yes">Port</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">False</property>
            <property name="xalign">0.5</property>
            <property name="draw_indicator">False</property>
          </object>
          <packing>
            <property name="left_attach">2</property>
            <property name="top_attach">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkCheckButton" id="port16">
            <property name="label" translatable="yes">Port</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">False</property>
            <property name="xalign">0.5</property>
            <property name="draw_indicator">False</property>
          </object>
          <packing>
            <property name="left_attach">3</property>
            <property name="top_attach">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkCheckButton" id="port17">
            <property name="label" translatable="yes">Port</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">False</property>
            <property name="xalign">0.5</property>
            <property name="draw_indicator">False</property>
          </object>
          <packing>
            <property name="left_attach">4</property>
            <property name="top_attach">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkCheckButton" id="port18">
            <property name="label" translatable="yes">Port</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">False</property>
            <property name="xalign">0.5</property>
            <property name="draw_indicator">False</property>
          </object>
          <packing>
            <property name="left_attach">5</property>
            <property name="top_attach">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkCheckButton" id="port19">
            <property name="label" translatable="yes">Port</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">False</property>
            <property name="xalign">0.5</property>
            <property name="draw_indicator">False</property>
          </object>
          <packing>
            <property name="left_attach">6</property>
            <property name="top_attach">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkCheckButton" id="port20">
            <property name="label" translatable="yes">Port</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">False</property>
            <property name="xalign">0.5</property>
            <property name="draw_indicator">False</property>
          </object>
          <packing>
            <property name="left_attach">7</property>
            <property name="top_attach">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkCheckButton" id="port21">
            <property name="label" translatable="yes">Port</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">False</property>
            <property name="xalign">0.5</property>
            <property name="draw_indicator">False</property>
          </object>
          <packing>
            <property name="left_attach">8</property>
            <property name="top_attach">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkCheckButton" id="port22">
            <property name="label" translatable="yes">Port</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">False</property>
            <property name="xalign">0.5</property>
            <property name="draw_indicator">False</property>
          </object>
          <packing>
            <property name="left_attach">9</property>
            <property name="top_attach">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkCheckButton" id="port23">
            <property name="label" translatable="yes">Port</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">False</property>
            <property name="xalign">0.5</property>
            <property name="draw_indicator">False</property>
          </object>
          <packing>
            <property name="left_attach">10</property>
            <property name="top_attach">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkCheckButton" id="port24">
            <property name="label" translatable="yes">Port</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">False</property>
            <property name="xalign">0.5</property>
            <property name="draw_indicator">False</property>
          </object>
          <packing>
            <property name="left_attach">11</property>
            <property name="top_attach">1</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

Адаптация вашего кода к файлу фиктивной поляны и внесение изменений для исправления кода:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import GObject as gobject
from gi.repository import Gtk, Gdk
import gtk


#Checkbuttons


def on_checkbutton_toggled(button, name):
    if (button.get_active()):
        state = "Active"

    else:
        state = "Inactive"
    print("port " + name + " toggled, state is " +state)


def start(button):
    print ("Button start clicked")

def save(button):
    print ("Button save clicked")

def clear(button):
    print ("Button clear clicked")

def select_all(button):
    print ("Button all clicked")

def details(button):
    print ("Button details clicked")

def click_for_help(button):
    print ("Button help clicked")

def gtk_style():
    css= b"""
.button:checked {
    color:white;
    background:red;
}
"""
    style_provider = Gtk.CssProvider()
    style_provider.load_from_data(css)

    Gtk.StyleContext.add_provider_for_screen(
        Gdk.Screen.get_default(),
        style_provider,
        Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
    )


# GUI (Graphical User Interface)
builder = Gtk.Builder()                                                                                                                                  
builder.add_from_file("myui.glade")                                                                                                         
handlers = {
        "action_start": start,
        "action_save": save,
        "action_clear": clear,
        "action_all": select_all,
        "action_details": details,
        "action_help":click_for_help


    }

builder.connect_signals(handlers)
window = builder.get_object("window1")

checkb1 = builder.get_object("port1")
checkb2 = builder.get_object("port2")
checkb3 = builder.get_object("port3")
checkb4 = builder.get_object("port4")
checkb5 = builder.get_object("port5")
checkb6 = builder.get_object("port6")
checkb7 = builder.get_object("port7")
checkb8 = builder.get_object("port8")
checkb9 = builder.get_object("port9")
checkb10 = builder.get_object("port10")
checkb11 = builder.get_object("port11")
checkb12 = builder.get_object("port12")
checkb13 = builder.get_object("port13")
checkb14 = builder.get_object("port14")
checkb15 = builder.get_object("port15")
checkb16 = builder.get_object("port16")
checkb17 = builder.get_object("port17")
checkb18 = builder.get_object("port18")
checkb19 = builder.get_object("port19")
checkb20 = builder.get_object("port20")
checkb21 = builder.get_object("port21")
checkb22 = builder.get_object("port22")
checkb23 = builder.get_object("port23")
checkb24 = builder.get_object("port24")

checkb1.connect ("toggled", on_checkbutton_toggled, "1")
checkb2.connect ("toggled", on_checkbutton_toggled, "2")
checkb3.connect ("toggled", on_checkbutton_toggled, "3")
checkb4.connect ("toggled", on_checkbutton_toggled, "4")
checkb5.connect ("toggled", on_checkbutton_toggled, "5")
checkb6.connect ("toggled", on_checkbutton_toggled, "6")
checkb7.connect ("toggled", on_checkbutton_toggled, "7")
checkb8.connect ("toggled", on_checkbutton_toggled, "8")
checkb9.connect ("toggled", on_checkbutton_toggled, "9")
checkb10.connect ("toggled", on_checkbutton_toggled, "10")
checkb11.connect ("toggled", on_checkbutton_toggled, "11")
checkb12.connect ("toggled", on_checkbutton_toggled, "12")
checkb13.connect ("toggled", on_checkbutton_toggled, "13")
checkb14.connect ("toggled", on_checkbutton_toggled, "14")
checkb15.connect ("toggled", on_checkbutton_toggled, "15")
checkb16.connect ("toggled", on_checkbutton_toggled, "16")
checkb17.connect ("toggled", on_checkbutton_toggled, "17")
checkb18.connect ("toggled", on_checkbutton_toggled, "18")
checkb19.connect ("toggled", on_checkbutton_toggled, "19")
checkb20.connect ("toggled", on_checkbutton_toggled, "20")
checkb21.connect ("toggled", on_checkbutton_toggled, "21")
checkb22.connect ("toggled", on_checkbutton_toggled, "22")
checkb23.connect ("toggled", on_checkbutton_toggled, "23")
checkb24.connect ("toggled", on_checkbutton_toggled, "24")
#---------------------------------------------------------------------------------------

window.connect("destroy", Gtk.main_quit)

gtk_style ()  # <----------------------- MOVED HERE

window.show_all()

Gtk.main()

Результат должен быть примерно таким:

порты

Другие вопросы по тегам