Python,GTK3,如何将禁用小部件的 View 从禁用 View 更改为正常 View ?

标签 python gtk3 gobject

在我将小部件状态更改为禁用之后。小部件的 View 正在更改为禁用 View 。我不想更改小部件的 View ,只更改小部件的状态。我怎样才能确保这一点?

谢谢。

btn = Gtk.Button("example")
btn.set_sensitive(False) # this code makes button disabled.
btn.set_view("normal") # I know set_view method does not exist but is there a method like this to change disabled view to normal view.

我的完整代码

import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk


class TitledEntry(Gtk.VBox):
    def __init__(self, title=None, text=""):
        Gtk.VBox.__init__(self, spacing=2)

        title = Gtk.Label(label=title, halign=Gtk.Align.START)
        self.add(title)

        entry = Gtk.Entry(text=text)
        self.add(entry)

        self.title_label = title
        self.entry = entry

        self.show_all()

class AgeBox(Gtk.HBox):
    def __init__(self, age=0):
        Gtk.HBox.__init__(self, halign=Gtk.Align.START)

        lb = Gtk.Label("Age : ")
        self.add(lb)

        agebutton = Gtk.SpinButton(adjustment=Gtk.Adjustment(age, 0, 1000, 1, 10, 0),
            numeric=True, update_policy=Gtk.SpinButtonUpdatePolicy.IF_VALID)
        agebutton.set_value(age)
        self.add(agebutton)

        self.agebutton = agebutton

class HumanTemp(Gtk.VBox):
    def __init__(self, nick="", age=0, country="", language=""):
        Gtk.VBox.__init__(self, spacing=3, border_width=5, halign=Gtk.Align.CENTER)

        temp_name = Gtk.Label()
        temp_name.set_markup("<span font_weight='bold'>Human</span>")
        self.pack_start(temp_name, False, False, 0)

        nick_box = TitledEntry("Nick", nick)
        self.pack_start(nick_box, False, False, 0)

        age_box = AgeBox(age)
        self.pack_start(age_box, False, False, 0)

        country_box = TitledEntry("Country", country)
        self.pack_start(country_box, False, False, 0)

        language_box = TitledEntry("Language", language)
        self.pack_start(language_box, False, False, 0)

        self.show_all()


class AnimTemp(Gtk.VBox):
    def __init__(self, nick="", age=0, kind="wolf"):
        Gtk.VBox.__init__(self, spacing=3, border_width=5, halign=Gtk.Align.CENTER)

        temp_name = Gtk.Label()
        temp_name.set_markup("<span font_weight='bold'>Animal</span>")
        self.pack_start(temp_name, False, False, 0)

        nick_box = TitledEntry("Nick", nick)
        self.pack_start(nick_box, False, False, 0)

        age_box = AgeBox(age)
        self.pack_start(age_box, False, False, 0)

        kind_box = Gtk.VBox()
        self.pack_start(kind_box, False, False, 0)

        kind_name = Gtk.Label("Kind", halign=Gtk.Align.START)
        kind_box.pack_start(kind_name, False, False, 0)

        box = Gtk.HBox()
        kind_box.pack_start(box, False, False, 0)

        wolf = Gtk.RadioButton("Wolf",group=None)
        box.pack_start(wolf, False, False, 0)

        tiger = Gtk.RadioButton("Tiger", group=wolf)
        box.pack_start(tiger, False, False, 0)

        if kind == "wolf":
            wolf.set_active(True)
        else:
            tiger.set_active(True)

        self.show_all()


class ElfTemp(Gtk.VBox):
    def __init__(self, nick="", age=0, clan="terran", race="night"):
        Gtk.VBox.__init__(self, spacing=3, border_width=5, halign=Gtk.Align.CENTER)

        temp_name = Gtk.Label()
        temp_name.set_markup("<span font_weight='bold'>Elf</span>")
        self.pack_start(temp_name, False, False, 0)

        nick_box = TitledEntry("Nick", nick)
        self.pack_start(nick_box, False, False, 0)

        age_box = AgeBox(age)
        self.pack_start(age_box, False, False, 0)

        clan_box = Gtk.VBox(halign=Gtk.Align.START)
        self.pack_start(clan_box, False, False, 0)

        clan_name = Gtk.Label("Clan", halign=Gtk.Align.START)
        clan_box.pack_start(clan_name, False, False, 0)

        box = Gtk.HBox()
        clan_box.pack_start(box, False, False, 0)

        terran = Gtk.RadioButton("Terran",group=None)
        box.pack_start(terran, False, False, 0)

        vanu = Gtk.RadioButton("Vanu", group=terran)
        box.pack_start(vanu, False, False, 0)

        atlas = Gtk.RadioButton("Atlas", group=terran, halign=Gtk.Align.CENTER)
        clan_box.pack_start(atlas, False, False, 0)

        race_box = Gtk.VBox()
        self.pack_start(race_box, False, False, 0)

        race_name = Gtk.Label("Race", halign=Gtk.Align.START)
        race_box.pack_start(race_name, False, False, 0)

        box = Gtk.HBox()
        race_box.pack_start(box, False, False, 0)

        night = Gtk.RadioButton("Night", group=None)
        box.pack_start(night, False, False, 0)

        blood = Gtk.RadioButton("Blood", group=night)
        box.pack_start(blood, False, False, 0)

        if clan == "terran":
            terran.set_active(True)
        elif clan == "vanu":
            vanu.set_active(True)
        else:
            atlas.set_active(True)

        if race == "night":
            night.set_active(True)
        else:
            blood.set_active(True)

        self.show_all()


class SelectTemplateWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="Select a Temlpate")

        self.resize(581, 506)

        scw = Gtk.ScrolledWindow(visible=True)
        self.add(scw)

        flbox = Gtk.FlowBox(min_children_per_line=2, valign=Gtk.Align.START)
        scw.add(flbox)

        keltas = ElfTemp("Keltas", 24, "terran", "blood")
        flbox.add(keltas)

        illidan = ElfTemp("Illidan", 47, "vanu", "night")
        flbox.add(illidan)

        jack = HumanTemp("Jack", 21, "Canada", "English")
        flbox.add(jack)

        santiago = HumanTemp("Santiago", 37, "Spain", "Spanish")
        flbox.add(santiago)

        moon_wolf = AnimTemp("Moon Wolf", 941, "wolf")
        flbox.add(moon_wolf)

        lexar = AnimTemp("Lexar", 438, "tiger")
        flbox.add(lexar)

        flbox.show_all()


win = SelectTemplateWindow()
win.connect("delete-event", Gtk.main_quit)
win.show()

Gtk.main()

我不想编辑 FlowBoxChild(模板),但是当我将 sensitive 设置为 False 时, View 正在改变( View 不好)。如果不设置对 False

敏感,我怎么能做到这一点

enter image description here

最佳答案

不确定这就是您想要的,但解决方法是将按钮打包在事件框内,然后切换 above_child 属性以获得所需的结果。当事件框位于按钮上方时,按钮将被禁止发出信号,否则将发出信号。

这是一个简单的例子:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class MyWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Hello World")

        self.box = Gtk.Box(spacing=6)
        self.add(self.box)

        self.button1 = Gtk.ToggleButton(label="Sensitive")
        self.button1.connect("clicked", self.on_button1_clicked)
        self.box.pack_start(self.button1, True, True, 0)

        self.evbox = Gtk.EventBox()
        self.button2 = Gtk.Button(label="Goodbye")
        self.button2.connect("clicked", self.on_button2_clicked)
        self.evbox.add(self.button2)
        self.evbox.set_above_child(True)
        self.box.pack_start(self.evbox, True, True, 0)

    def on_button1_clicked(self, widget):
        self.evbox.set_above_child(not self.button1.get_active())

    def on_button2_clicked(self, widget):
        print("Goodbye")

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

Sensitive 切换按钮将控制右侧的按钮,同时即使在“不敏感”时也能保持其外观。事实上,您只是在控制事件框属性。

将按钮打包在事件框内:

...
self.evbox = Gtk.EventBox()
self.button2 = Gtk.Button(label="Goodbye")
self.evbox.add(self.button2)
self.evbox.set_above_child(True) # Default is not sensitive
...

为了和toggle button保持一致,默认是设置button为不敏感的,那么在toggle button回调上,我们改变事件框属性,相当于对toggle button的toggle属性进行逻辑取反:

def on_button1_clicked(self, widget):
    self.evbox.set_above_child(not self.button1.get_active())

使用此解决方案就像在按钮的父容器上调用单个方法 set_above_child 一样简单。

关于Python,GTK3,如何将禁用小部件的 View 从禁用 View 更改为正常 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47313817/

相关文章:

c - 学习 GObject 和 Glib 的 Material

python - 使用特定值改变条形图中每个条形的颜色

c - 使用不是 GObject 属性的参数初始化 GObject?

python - 计算pandas中相应值的频率[python 3]

python - Glade GTK3 Python TreeView 切换不会切换

python - 带有 pygobject 的 pango 属性

无法使用 gtk-lib 编译简单的 C 程序

c - 如何创建具有公共(public)和私有(private)成员的 GObject 最终类?

Python 语句作为方法的参数

python - 嵌套列表的 Groupby