我试图在Gtk窗口中绘制一个圆阵列。我可以在Gtk.DrawingArea中绘制一个,当DrawingArea是唯一的对象时,它会扩展为适合窗口。但是,当我在Gtk.Grid中放置多个时,它们无法扩展以填充网格。
如何让它们填满网格?
我回顾了this post,它提到了this page,但他们没有解决问题(或者我没有掌握这个概念)。
我试图将属性expand,hexpand,vexpand,hexpand_set和vexpand_set设置为True,并将set_halign和set_valign设置为Gtk.Align.FILL无效
我的圈子是通过CircleArea.py创建的
from gi.repository import Gtk
import cairo
import math
class CircleArea(Gtk.DrawingArea):
"""Establishes the space for the circle and paints the circle in it"""
def __init__(self):
super(CircleArea, self).__init__()
self.hexpand = True
self.vexpand = True
self.set_halign = Gtk.Align.FILL
self.set_valign = Gtk.Align.FILL
self.connect('draw', self.on_draw)
def on_draw(self, widget, cr):
height = widget.get_allocated_height()
width = widget.get_allocated_width()
smaller = width if width < height else height
cr.set_source_rgb(self.red, self.green, self.blue)
cr.arc(height / 2, width / 2, smaller * 0.45, 0, 2 * math.pi)
cr.fill()
窗口本身在Grid.py中
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from CircleArea import CircleArea
class CircleWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Circle Grid")
self.set_border_width(10)
self.grid = Gtk.Grid()
self.circle_area1 = CircleArea()
self.circle_area2 = CircleArea()
self.grid.attach(self.circle_area1, 0, 0, 1, 1)
self.grid.attach(self.circle_area2, 1, 0, 1, 1)
self.add(self.grid)
win = CircleWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
我希望圆圈填充可用的网格空间,但它们的大小都是1x1。
我的问题是set_halign,set_valign,set_hexpand和set_vexpand是方法而不是属性。所以在CirleArea.init()中,我将代码更改为:
def __init__(self):
super(CircleArea, self).__init__()
self.set_hexpand(True)
self.set_vexpand(True)
self.set_halign(Gtk.Align.FILL)
self.set_valign(Gtk.Align.FILL)
self.connect('draw', self.on_draw)