【问题标题】:Create a custom widget with PyGObject使用 PyGObject 创建自定义小部件
【发布时间】:2015-06-23 01:05:16
【问题描述】:

我正在尝试使用 PyGObject 创建自定义小部件。例如,我想创建这个CustomButton 小部件,它在按钮中添加图像和标签(仅用于示例):

#!/usr/bin/python
#-*- coding: utf-8 -*

from gi.repository import Gtk

class ImageButton(Gtk.Widget):

    def __init__(self, label, image):
        Gtk.Widget.__init__(self)

        self.hbox = Gtk.HBox()
        self.label = Gtk.Label(label)
        self.image = Gtk.Image.new_from_stock(image, Gtk.IconSize.MENU)

        self.hbox.pack_start(self.image, False, False, 3)
        self.hbox.pack_start(self.label, False, False, 3)

        self.button = Gtk.Button()
        self.button.add(self.hbox)

在另一个文件或类中,我可以这样使用它:

button = ImageButton("My label", Gtk.STOCK_HOME)

但是当我想使用它时,我不得不调用button属性,像这样:

# Connect the "clicked" event 
button.button.connect("clicked", on_clicked_button)

# Add the button in a container
window.add(button.button)

有效但不实用。如何创建一个像任何其他小部件一样工作的自定义小部件:

button = ImageButton("My label", Gtk.STOCK_HOME)
button.connect("clicked", on_clicked_button)
window.add(button)

【问题讨论】:

  • 你为什么不能写button.button
  • 有可能,但我想创建一个真正的自定义小部件。我不想写button.button.connect("clicked", on_clicked_button),只想写button.connect("clicked", on_clicked_button)
  • 所以写一个connect方法...
  • 我真的不明白为什么你有一个按钮属性并且继承自Button
  • 我有一个按钮属性并继承自Button,因为我认为这是一个很好的解决方案,但如果你有答案,我会接受 :)

标签: python gtk3 pygobject


【解决方案1】:

我认为你的问题实际上是关于理解类而不是继承的问题。如果您希望您的小部件完全充当按钮,它应该是一个按钮。

看看下面的例子:

from gi.repository import Gtk

class ImageButton(Gtk.EventBox):

    def __init__(self, label):

        Gtk.EventBox.__init__(self)
        self.label = Gtk.Label(label)
        self.add(self.label)    


if __name__ == '__main__':
    def on_imagebutton_clicked(button, data=None):
        print("Button has been clicked!")

    window = Gtk.Window()
    button = ImageButton("My label")
    button.connect('button-press-event', on_imagebutton_clicked)
    window.add(button)
    window.show_all()
    Gtk.main()

我没有说我的班级是Gtk.Widget,而是说它是Gtk.EventBox,我就这样开始了。从现在开始,ImageButton 将拥有与Gtk.EventBox 相同的属性和方法。

*如果我通过使用Gtk.Button 而不是Gtk.EventBox 来制作相同的示例您可以调用 button.connect(.. 而不是buton.connect.connect(..,因为您希望在您的问题中使用它。这样做的问题是,如果ImageButtonGtk.Button,则不再可能修改它以执行按钮不执行的操作(例如添加容器和标签)。

简而言之:

您可以基于其他小部件创建自定义小部件,但只有一个小部件位于树的顶部。

--> Parent  
---------> Child
---------> Child

因此,当您执行 self.method 时,它总是会查看:

1) 您的父方法(您使用 Gtk.EventBox.__init__(self) 复制的方法

2) 您创建的方法。

另外,您也可以因此撒谎:

from gi.repository import Gtk

class ImageButton(Gtk.EventBox):

    def __init__(self, label):

        Gtk.EventBox.__init__(self)
        self.label = Gtk.Label(label)
        self.add(self.label)    

    def set_text(self, text):
        self.label.set_text(text)

if __name__ == '__main__':
    def on_imagebutton_clicked(button, data=None):
        print("Button has been clicked!")

    window = Gtk.Window()
    button = ImageButton("My label")
    button.connect('button-press-event', on_imagebutton_clicked)
    button.set_text('New text!!')
    window.add(button)
    window.show_all()
    Gtk.main()

请注意,我不必致电button.label.set_text(..) 我希望它足够清楚!

【讨论】:

    猜你喜欢
    • 2012-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-08
    • 2023-03-20
    • 2019-11-20
    • 1970-01-01
    相关资源
    最近更新 更多