如何获取Gtk.DrawingArea填充Gtk.Grid?

时间:2019-04-17 18:56:26

标签: python gtk pygobject

我正在尝试在Gtk窗口中绘制一组圆形。我可以在Gtk.DrawingArea中绘制一个,当DrawingArea是唯一的对象时,它会扩展以适合窗口。但是,当我将多个放入Gtk.Grid时,它们无法扩展以填充Grid。

如何让他们填充网格?

我查看了涉及this postthis page,但他们没有解决问题(或者我未能理解这个概念)。

我试图将属性expand,hexpand,vexpand,hexpand_set和vexpand_set设置为True,将set_halign和set_valign设置为Gtk.Align.FILL无效

我的圈子是通过CircleArea.py创建的

const departments = [{"departmentNumber":"1","departmentName":"food"},{"departmentNumber":"2","departmentName":"beverage"},{"departmentNumber":"3","departmentName":"apparel"}];

departments.forEach(value => {
  console.log(value.departmentName)
  console.log(value.departmentNumber)
})

窗口本身在Grid.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()

我希望圆圈填充可用的网格空间,但是它们的大小均为1x1。

1 个答案:

答案 0 :(得分:0)

我的问题是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)