【问题标题】:Failing to draw on a Gtk.DrawingArea未能在 Gtk.DrawingArea 上绘图
【发布时间】:2014-07-15 14:56:47
【问题描述】:

我不会画画,我已经阅读了教程,我找不到问题。 我只是有一个由 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()

【问题讨论】:

  • 您使用的是 GTK+ 2 还是 3?
  • 3 因为我正在使用 gi.repository

标签: gtk drawing drawable pygtk drawingarea


【解决方案1】:

你不见了

self.__Grid.show()

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

一般来说,在某个顶级容器上调用show_all() 比尝试记住每个单独的小部件show() 更容易。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-27
    • 2014-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-09
    相关资源
    最近更新 更多