我目前正在開發一個PyGObject應用程序,我在Gtk + FlowBox中選擇特定的子項時遇到問題。即使在選擇FlowBox選擇模式(SINGLE)填充FlowBox並編寫代碼以選擇特定的子項後,始終選擇第一個子項。Gtk + 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 +? – andlabs
我使用Ubuntu 16.04,所以Gtk + 3.18。 –