无法在GTK中垂直打包按钮

it_*_*ure 2 python gtk

我想在盒子里垂直打包两个按钮,为什么它不起作用?这是我的代码.

from gi.repository import Gtk
class MyWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="Hello World")
        self.box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
        self.box = Gtk.Box(spacing=6)
        self.add(self.box)
        self.button1 = Gtk.Button(label="Hello")
        self.box.pack_start(self.button1, True, True, 0)
        self.button2 = Gtk.Button(label="Goodbye")
        self.box.pack_start(self.button2, True, True, 0)


win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
Run Code Online (Sandbox Code Playgroud)

pto*_*ato 6

你创建self.box一个垂直方向的盒子,然后立即用另一个完全不同的新盒子覆盖它,间隔为6(和默认的水平方向).相反

self.box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
Run Code Online (Sandbox Code Playgroud)