【问题标题】:Gtk+ FlowBox selection not workingGtk+ FlowBox 选择不起作用
【发布时间】:2016-08-22 00:02:46
【问题描述】:

我目前正在开发 PyGObject 应用程序,但在选择 Gtk+ FlowBox 中的特定子项时遇到问题。即使在选择 FlowBox 选择模式 (SINGLE) 填充 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 处选择子项,该应用程序也只会显示第一个被选中的子项: Screenshot of above code running

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

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

【问题讨论】:

  • GTK+ 的具体版本是什么?
  • 我使用的是 Ubuntu 16.04,所以 Gtk+ 3.18。

标签: python gtk gtk3 gnome pygobject


【解决方案1】:

我认为您在 GTK 中找到了一个错误,似乎show_all 中的某些东西搞砸了。我的第一个猜测是,这是由于 FlowBox 没有实现,所以我将您的代码更改为使用 show 信号(realize 但稍后发出 show)并检查它是否仍然发生.可悲的是……

所以我觉得有别的东西不对劲所以只是在Gtk.Window.__init__ 之后添加了一个快速测试self.show() 这使得选择工作但使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 开发人员。
猜你喜欢
  • 2020-05-01
  • 2017-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-26
相关资源
最近更新 更多