Python Gtk&x2B;流框选择不起作用

Python Gtk&x2B;流框选择不起作用,python,gtk,gtk3,gnome,pygobject,Python,Gtk,Gtk3,Gnome,Pygobject,我目前正在开发PyGObject应用程序,在Gtk+流框中选择特定的子项时遇到问题。即使在选择FlowBox选择模式(单一)填充FlowBox并编写代码以选择特定子项之后,也始终选择第一个子项 #!/usr/bin/python import gi gi.require_version('Gtk', '3.0') from gi.repository import Gtk, Gio class App(Gtk.Window): def __init__(self):

我目前正在开发PyGObject应用程序,在Gtk+流框中选择特定的子项时遇到问题。即使在选择FlowBox选择模式(单一)填充FlowBox并编写代码以选择特定子项之后,也始终选择第一个子项

#!/usr/bin/python

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gio

class App(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="App")

        flowbox = Gtk.FlowBox()
        flowbox.set_valign(Gtk.Align.START)
        flowbox.set_selection_mode(Gtk.SelectionMode.SINGLE)

        # Drawing 3 squares
        flowbox.add(self.drawing_area())
        flowbox.add(self.drawing_area())
        flowbox.add(self.drawing_area())

        child = flowbox.get_child_at_index(2)
        flowbox.select_child(child)
        flowbox.queue_draw()

        self.add(flowbox)

    def drawing_area(self):
        preview = Gtk.DrawingArea()
        preview.connect("draw", self.draw_square)
        preview.set_size_request(150, 150)
        return preview

    def draw_square(self, widget, cr):
        cr.scale(150, 150)

        style_context = widget.get_style_context()
        color = style_context.get_color(Gtk.StateFlags.NORMAL)
        cr.set_source_rgba(*color)

        cr.rectangle(0, 0, 1, 1)
        cr.fill()

window = App()
window.connect("delete-event", Gtk.main_quit)
window.show_all()
Gtk.main()
即使我选择在索引2中选择子项,应用程序也只会显示第一个被选择的子项:

奇怪的是,当我使用以下代码(放在“self.add(flowbox)”行之前)检查选择了哪个子项时,终端显示我指定要选择的子项(在索引2处)是唯一选择的子项,即使窗口仅显示选择的第一个子项:

for child in flowbox.get_selected_children():
    print child.get_index()

我想您已经在GTK中找到了一个bug,似乎
show\u all
中的某些东西出了问题。我的第一个猜测是,这是因为
流框
未实现,所以我将您的代码更改为使用
显示
信号(
实现
,但
显示
稍后发出),并检查它是否仍然发生。不幸的是

因此,我觉得有些东西被关闭了,所以只需在
Gtk.Window之后添加一个快速测试
self.show()
。\uuu init\uuu
这使得选择工作正常,但使
Flowbox
比需要的更宽(可能是因为默认的空窗口宽度)。因此,我在侦听器中添加了
self.show()
,这实际上解决了这个问题

完整的代码如下所示,但由于这是一个肮脏的解决方法,您仍然应该报告此错误

#!/usr/bin/python

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gio

class App(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="App")

        self.flowbox = Gtk.FlowBox()
        self.flowbox.set_valign(Gtk.Align.START)
        self.flowbox.set_selection_mode(Gtk.SelectionMode.SINGLE)

        # Drawing 3 squares
        self.flowbox.add(self.drawing_area())
        self.flowbox.add(self.drawing_area())
        self.flowbox.add(self.drawing_area())

        self.flowbox.connect("show", self.on_realize)

        self.add(self.flowbox)

    def on_realize(self, flowbox):
        # The creative workaround/hack
        self.show()
        child = self.flowbox.get_child_at_index(2)
        self.flowbox.select_child(child)

    def drawing_area(self):
        preview = Gtk.DrawingArea()
        preview.connect("draw", self.draw_square)
        preview.set_size_request(150, 150)
        return preview

    def draw_square(self, widget, cr):
        cr.scale(150, 150)

        style_context = widget.get_style_context()
        color = style_context.get_color(Gtk.StateFlags.NORMAL)
        cr.set_source_rgba(*color)

        cr.rectangle(0, 0, 1, 1)
        cr.fill()

window = App()
window.connect("delete-event", Gtk.main_quit)
window.show_all()
Gtk.main()

GTK+的具体版本是什么?我使用的是Ubuntu 16.04,所以GTK+3.18。谢谢!这解决了我程序中的问题。我将向Gtk开发人员报告此错误。