Python,GTK3,如何从禁用视图将禁用窗口小部件的视图更改为普通视图?

时间:2017-11-15 17:31:39

标签: python gtk3 gobject

我将窗口小部件状态更改为禁用后。小部件视图正在更改为禁用视图。我不想改变小部件的视图只改变小部件的状态。我该如何确保这一点?

感谢。

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(模板),但是当我对False设置敏感时,视图正在改变(视图不好)。如何在不设置对False

的敏感的情况下执行此操作

enter image description here

1 个答案:

答案 0 :(得分:1)

不确定这是你所追求的,但解决方法是将按钮打包在一个事件框中,然后切换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()

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

将按钮打包在事件框中:

...
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
...

为了与切换按钮保持一致,默认情况下将按钮设置为不敏感,然后在切换按钮回调中,我们更改事件框属性,这相当于切换按钮的切换属性的逻辑否定:

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

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