python gtk页面布局,gtk python网格调整大小

Following the official tutorials about containers.

I have the follwoing code

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()

All good when I run it

298a5573c147d540b5220d15c286166f.png

However if I resize it the grid won't resize with the main window.

Any pointers how to make the grid resize?

0e292fd627b1b30dcabcc56987d1b0fb.png

thanks

解决方案

Set the buttons to expand. Example:

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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值