gpt4 book ai didi

python - 从 Gtk.Widget 派生更好还是直接从特定小部件派生更好?

转载 作者:太空宇宙 更新时间:2023-11-03 16:19:18 25 4
gpt4 key购买 nike

当我为我的应用程序创建 UI 类时,我经常想知道是否最好从 Gtk.Widget 派生我的类,然后显式添加我需要的小部件,或者直接从特定小部件派生.

这里有两个例子,哪一个最好?

class MyComponent(Gtk.Widget):
def __init__(self):
Gtk.Widget.__init__(self)

box = Gtk.Box.new(Gtk.Orientation.HORIZONTAL, 6)
button1 = Gtk.Button.new_with_label("Awesome Button1")
button2 = Gtk.Button.new_with_label("Awesome Button2")
button3 = Gtk.Button.new_with_label("Awesome Button3")
box.pack_start(button1, True, True, 0)
box.pack_start(button2, True, True, 0)
box.pack_start(button3, True, True, 0)

self.add(box)
class MyComponent(Gtk.Box):
def __init__(self):
Gtk.Box.__init__(self, Gtk.Orientation.HORIZONTAL, 6)

button1 = Gtk.Button.new_with_label("Awesome Button1")
button2 = Gtk.Button.new_with_label("Awesome Button2")
button3 = Gtk.Button.new_with_label("Awesome Button3")
self.pack_start(button1, True, True, 0)
self.pack_start(button2, True, True, 0)
self.pack_start(button3, True, True, 0)

在我看来,第一个版本更好,因为我的组件在技术上不是 Gtk.Box,它是一个使用 Gtk.Box 的小部件,但它也可以使用 Gtk.GridGtk.DrawingArea

所以从设计的角度来看这似乎是正确的,但也许有一些我没有看到的技术细节。你有什么建议?

最佳答案

这取决于你头脑中的抽象。问问自己:您的组件仅仅是一个定制的盒子还是更多的东西?

此外,您实际上这里需要继承吗?也许您可以取消一个免费的工厂函数,该函数根据您的喜好创建一组 Gtk.Widgets。

关于python - 从 Gtk.Widget 派生更好还是直接从特定小部件派生更好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38650893/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com