python - gtk python 网格调整大小

标签 python linux user-interface gtk3

遵循有关容器的官方教程。 我有以下代码

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class MainWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="test")

        grid = Gtk.Grid()
        self.add(grid)

        button1 = Gtk.Button(label="Button 1")
        button2 = Gtk.Button(label="Button 2")
        button3 = Gtk.Button(label="Button 3")
        button4 = Gtk.Button(label="Button 4")
        button5 = Gtk.Button(label="Button 5")
        button6 = Gtk.Button(label="Button 6")

        grid.add(button1)
        grid.attach(button2, 1, 0, 2, 1)
        grid.attach_next_to(button3, button1, Gtk.PositionType.BOTTOM, 1, 2)
        grid.attach_next_to(button4, button3, Gtk.PositionType.RIGHT, 2, 1)
        grid.attach(button5, 1, 2, 1, 1)
        grid.attach_next_to(button6, button5, Gtk.PositionType.RIGHT, 1, 1)


win = MainWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()

运行时一切正常

enter image description here

但是,如果我调整它的大小,网格将不会随主窗口一起调整大小。

任何指针如何使网格调整大小? enter image description here

谢谢

最佳答案

设置按钮展开。示例:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class MainWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="test")

        grid = Gtk.Grid()
        self.add(grid)

        button1 = Gtk.Button(label="Button 1", expand = True)
        button2 = Gtk.Button(label="Button 2", expand = True)
        button3 = Gtk.Button(label="Button 3", expand = True)
        button4 = Gtk.Button(label="Button 4", expand = True)
        button5 = Gtk.Button(label="Button 5", expand = True)
        button6 = Gtk.Button(label="Button 6", expand = True)

        grid.add(button1)
        grid.attach(button2, 1, 0, 2, 1)
        grid.attach_next_to(button3, button1, Gtk.PositionType.BOTTOM, 1, 2)
        grid.attach_next_to(button4, button3, Gtk.PositionType.RIGHT, 2, 1)
        grid.attach(button5, 1, 2, 1, 1)
        grid.attach_next_to(button6, button5, Gtk.PositionType.RIGHT, 1, 1)


win = MainWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()

关于python - gtk python 网格调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54165638/

相关文章:

iOS UI - 类似于 iPhoto 底栏 UI

python - Unix 排序产生错误的输出

python - 使用 python 将 ssl 包含到请求中

Java运行时执行程序有延迟

linux - 1 小时前修改的文件列表

c - 来自多个线程的相同 fd 上的 `select`

linux - 无法获取 .screenrc,它说找不到命令

python - 加载 Jupyter Notebook 扩展时出错

java - 创建可视化图表

C++ GUI 和控制台应用程序