2016-08-22 67 views
1

我目前正在開發一個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() 
+0

什麼特定版本的GTK +? – andlabs

+0

我使用Ubuntu 16.04,所以Gtk + 3.18。 –

回答

1

我想你已經找到了一個GTK中的錯誤,似乎show_all中的東西搞亂了。我的第一個猜測是,這是因爲FlowBox沒有實現,所以我更改了代碼以使用show信號(realize,但show稍後發出),並檢查它是否仍然發生。可悲的是..

所以我的感覺是,別的東西是關閉,因此僅僅是一個快速增加的測試self.show()Gtk.Window.__init__之後本作的選擇工作,但比需要的(可能是因爲一個默認的寬度更寬的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() 
+0

謝謝!這解決了我的計劃中的問題。我會把這個bug報告給Gtk開發者。 –