无法在Gtk.DrawingArea上绘制

时间:2014-07-15 14:56:47

标签: gtk drawing drawable pygtk drawingarea

我无法画画,我已经阅读了教程,我找不到问题。 我只是有一个由Glade绘制的正确UI。然后我想绘制,例如50个绘图区域。所以我创建了一个包含50个单元格的Grid;对于每个单元格,有一个垂直框(每个框内都有一个绘图区域和一个标签)。但是我看不到任何画面。

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

1 个答案:

答案 0 :(得分:1)

你错过了

self.__Grid.show()

因此,网格中没有任何内容显示出来。

一般情况下,只需在某个顶级容器上调用show_all(),而不是试图记住show()每个小部件,就更容易了。