2010-05-16 1 views
0

다음 PyGTk 코드는 활성 항목이없는 콤보 상자를 제공합니다. 기본값 인 을 원하지 않고 사용자가 선택하도록하는 경우가 있습니다.활성 항목이없는 gtk.ComboBox의 텍스트

여전히 같은 빈 콤보 바 쇼 뭔가가 할 수있는 방법이있다 : 더미 항목을 추가하지 않고 "항목 ... 선택"?

import gtk 
import sys 
say = sys.stdout.write 

def cb_changed(w): 
    say("Active index=%d\n" % w.get_active()) 

topwin = gtk.Window() 
topwin.set_title("No Default") 
topwin.set_size_request(0x100, 0x20) 
topwin.connect('delete-event', gtk.main_quit) 
vbox = gtk.VBox() 

ls = gtk.ListStore(str, str) 
combo = gtk.ComboBox(ls) 
cell = gtk.CellRendererText() 
combo.pack_start(cell) 
combo.add_attribute(cell, 'text', 0) 
combo.connect('changed', cb_changed) 

ls.clear() 
map(lambda i: ls.append(["Item-%d" % i, "Id%d" % i]), range(3)) 

vbox.pack_start(combo, padding=2) 
topwin.add(vbox) 
topwin.show_all() 

gtk.main() 
say("%s Exiting\n" % sys.argv[0]) 
sys.exit(0) 

답변

0

앞서 거대한 해킹 (나는 당신의 프로그램이 추가) :

import gtk 
import sys 
say = sys.stdout.write 

def cb_changed(w): 
    say("Active index=%d\n" % w.get_active()) 

topwin = gtk.Window() 
topwin.set_title("No Default") 
topwin.set_size_request(0x100, 0x20) 
topwin.connect('delete-event', gtk.main_quit) 
vbox = gtk.VBox() 

ls = gtk.ListStore(str, str) 
combo = gtk.ComboBox(ls) 
cell = gtk.CellRendererText() 
combo.pack_start(cell) 
combo.add_attribute(cell, 'text', 0) 
combo.connect('changed', cb_changed) 

#- Begin of the hack ---------------------------------- 
def special_empty_text (cell_view, event): 
    if cell_view.window is None: 
     return False 

    row = cell_view.get_displayed_row() 
    if row is not None: 
     return False 

    layout = cell_view.create_pango_layout ('bla bla bla') 
    context = cell_view.window.cairo_create() 

    xpad = 0 
    ypad = 0 

    renderer = cell_view.get_cells() [0] 
    if renderer is not None: 
     xpad = renderer.props.xpad 
     ypad = renderer.props.ypad 

    context.move_to (cell_view.allocation.x + xpad, cell_view.allocation.y + ypad) 
    context.set_source_rgb (0.6, 0.6, 0.6) 
    context.show_layout (layout) 

    return True 

combo.child.connect ('expose-event', special_empty_text) 
#- End of the hack ---------------------------------- 

ls.clear() 
map(lambda i: ls.append(["Item-%d" % i, "Id%d" % i]), range(3)) 

vbox.pack_start(combo, padding=2) 
topwin.add(vbox) 
topwin.show_all() 

gtk.main() 
say("%s Exiting\n" % sys.argv[0]) 
sys.exit(0) 

이 더 좋은 방법이 표시되지 않습니다.