无法在Gtk.DrawingArea上绘制 [英] Failing to draw on a Gtk.DrawingArea

查看:93
本文介绍了无法在Gtk.DrawingArea上绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法绘图,我已经阅读了教程,找不到问题了。
我只是有Glade绘制的正确UI。然后我要绘制50个绘制区域。因此,我创建了一个包含50个单元的网格;对于每个单元格,都有一个垂直框(每个框内都有一个绘图区域和一个标签)。

I'm not able to draw, i've already read tutorials, i can't find the problem. I simply have a correct UI drawed by Glade. Then i want to draw, for example 50 drawing areas. So i create a Grid with 50 cells; for each cell there is a vertical box (with a drawing area and a label inside each one). But i can't seen anything drawed.

class collega_GUI:


    def __init__(self):

        try:

            self.__builder = Gtk.Builder()
            self.__builder.add_from_file('UI2.glade')

            self.__Grid = Gtk.Grid()
            self.__Grid.set_margin_left(20)
            self.__Grid.set_margin_right(20)
            self.__Grid.set_row_spacing(10)
            self.__Grid.set_column_spacing(15)
            self.__Grid.set_column_homogeneous(True)
            self.__GridBox = self.__builder.get_object('box11')
            self.__GridBox.pack_end(self.__Grid, 1, 1, 20)

            indirizzi_ip = []
            for i in range(50):
                indirizzi_ip.append(str(i))
            cpu_info = {}
            for ip in indirizzi_ip:
                cpu_info[ip] = dict()
            left = 0
            right = 0

            for ip in indirizzi_ip:
                cpu_info[ip]['drawing_area'] = Gtk.DrawingArea()
                cpu_info[ip]['drawing_area'].set_size_request(100, 100)
                cpu_info[ip]['drawing_area'].set_name(ip)

                box = Gtk.VBox(False, 5)
                box.add(cpu_info[ip]['drawing_area'])
                label = Gtk.Label(ip)
                box.add(label)

                self.__Grid.attach(box, left, right, 1, 1) #object,left,right,top,bottom
                cpu_info[ip]['drawing_area'].connect("draw", self.__draw)
                label.show()
                cpu_info[ip]['drawing_area'].show() #the draw should start now!
                box.show()

                # 5 drawing areas in a row
                left += 1
                if left == 5:
                    right += 1
                    left = 0

            self.__builder.get_object('Azioni_Window').show()
            Gtk.main()

        except Exception as xe:
            logging.error('%s' % str(xe))
            sys.exit()


    def __draw(self, widget, context):

        context.set_source_rgb(0.9, 0, 0.1) #rosso
        context.set_source_rgb(0.1, 0.9, 0) #verde
        context.set_source_rgb(0.8, 0.7, 0) #giallo
        context.set_source_rgb(0.8, 0.7, 0.8) #inattivo
        context.rectangle(0, 0, widget.get_allocated_width(), widget.get_allocated_height())
        context.fill()


if __name__=='__main__':

    try:
        UINX=collega_GUI()
    except Exception:
        sys.exit()


推荐答案

您丢失了

self.__Grid.show()

因此不会显示网格中的任何内容。

And hence nothing in the grid is shown.

通常,只需调用 show_all()放在某个顶级容器上,而不是试图记住每个单独的小部件 show()

In general it's easier to just call show_all() on some top-level container rather than trying to remember to show() every individual widget.

这篇关于无法在Gtk.DrawingArea上绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆