【问题标题】:Python, GTK3, How can I change view of disabled widget to normal view from disabled view?Python,GTK3,如何将禁用小部件的视图从禁用视图更改为普通视图?
【发布时间】:2018-04-29 01:21:15
【问题描述】:

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

谢谢。

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敏感,我怎么能做到这一点

【问题讨论】:

  • 你能发布一个 MCVE 吗?
  • @theGtknerd,我编辑了
  • “正常”是什么意思?让它再次敏感?为此,您将使用btn.set_sensitive(True)
  • @SvenFestersen,禁用模式下的小部件看起来与正常模式不同,我只想更改禁用模式下的小部件视图而不更改禁用模式。
  • @Nomad:只是为了确保我做对了:您希望按钮看起来很敏感但对点击没有反应?这对您的用户来说非常具有误导性(并且可能令人沮丧),我建议不要这样做。但是,如果您想这样做,我认为您可以将样式从活动状态按钮转移到禁用按钮。我不知道如何详细地做到这一点。这个问题可能包含一些提示:stackoverflow.com/questions/38158289/…

标签: python gtk3 gobject


【解决方案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
...

为了和toggle按钮一致,默认设置按钮为不敏感,然后在toggle button回调上,我们改变event box属性,相当于toggle button的toggle属性逻辑取反:

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

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

【讨论】:

  • 非常感谢您的评论。但我很久以前就这样做了。是的EventBox 阻止鼠标操作。但是我仍然可以通过按Enter 键并使用键盘单击按钮,我可以通过这种方式做很多事情。我想要完全不敏感但具有正常视图的按钮。
  • @Nomad 确实,键盘事件确实有效,但可以通过不允许焦点和过早处理键盘操作来抑制,但对于一个简单的要求来说似乎需要做很多工作。我会尝试调查它,但正如其他人所说,这不是按钮的正常行为。
  • @Nomad 我认为唯一的方法是使用 css 类。覆盖残疾人班,但似乎很多工作收效甚微。我已经使用 Gtk Inspector 来玩 css 并且确实设法更改了禁用小部件的 ui 属性,但我建议您使用不同的方法。
猜你喜欢
  • 2018-01-06
  • 1970-01-01
  • 1970-01-01
  • 2014-07-30
  • 1970-01-01
  • 2015-09-28
  • 1970-01-01
  • 2012-04-26
  • 1970-01-01
相关资源
最近更新 更多