gtk.ComboBox 中没有活动项目的文本

发布于 2024-09-01 09:53:57 字数 804 浏览 1 评论 0原文

以下 PyGTk 代码给出了一个没有活动项目的组合框。 这适用于我们不希望有默认值的情况, 并强制用户选择。

不过,有没有办法让空的组合栏显示类似以下内容: “选择一个项目...” 不添加虚拟物品?

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)

The following PyGTk code, gives a combo-box without an active item.
This serves a case where we do not want to have a default,
and force the user to select.

Still, is there a way to have the empty combo-bar show something like:
"Select an item..."
without adding a dummy item?

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

舟遥客 2024-09-08 09:53:57

巨大的黑客攻击(我刚刚将其添加到您的程序中):

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)

没有看到更好的方法。

Huge hack ahead (I just added this to your program):

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)

Don't see a nicer way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文