【发布时间】: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