1

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

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

1 回答 1

1

你不见了

self.__Grid.show()

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

一般来说,只调用show_all()一些顶级容器而不是尝试记住show()每个单独的小部件更容易。

于 2014-07-15T15:11:59.317 回答