如何在 Ruby Glade/GTK 中制作多选列表框,也许使用 TreeView?

发布于 2024-07-30 15:06:19 字数 469 浏览 9 评论 0原文

我正在尝试在glade/ruby 程序中创建一个多选列表框,我想知道如何执行此操作,例如我添加到窗口的元素以及我可以使用的相应示例代码。 我正在查看 GTKList 但它说它现在已被弃用,而且我也不知道如何让它在 ruby​​ 中工作。 GTK List 文档说使用 TreeView,但我不知道如何设置。

需要明确的是,我想要这样的东西,用户可以选择多个条目:

替代文本 http://geekswithblogs.net/images/geekswithblogs_net/dotNETvinz/OutputPreselectListBox.JPG

感谢大家的帮助! 对于这个问题我真的很绝望。

I am trying to make a multiple-select listbox in glade/ruby program and I am wondering how I go about doing this, like what element I add to the window and the corresponding example code I can use. I was looking at GTKList but it says it is deprecated now, and also I don't know how to get it working in ruby. GTK List docs say to use TreeView, but I have no idea how to set that up.

Just to be clear, I would like something like this, where the user can select multiple entries:

alt text http://geekswithblogs.net/images/geekswithblogs_net/dotNETvinz/OutputPreselectListBox.JPG

Thanks for the help guys! I am really desperate on this question.

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

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

发布评论

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

评论(2

弱骨蛰伏 2024-08-06 15:06:23

上面的答案是正确的,但是如果您使用 VisualRuby 编写它会容易得多:

class MyList < VR::Listview

  def initialize(:employee_names => String)
    add_row(:employee_names => "Vince")
    add_row(:employee_names => "Jhen")
    add_row(:employee_names => "Chris")
    add_row(:employee_names => "Shynne")
  end

end

这会将所有内容设置得与您的示例完全相同,包括标题。

然后,您只需将其添加到框或滚动窗口中即可:

class GUI

  include GladeGUI

  def initialize()
    load_glade(__FILE__)
    @builder("Scrolledwindow1").add(MyList.new)
    show_window()
  end

end

转到:

http://www.visualruby.net

THe above answer is correct, but if you wrote it using visualruby it would be a lot easier:

class MyList < VR::Listview

  def initialize(:employee_names => String)
    add_row(:employee_names => "Vince")
    add_row(:employee_names => "Jhen")
    add_row(:employee_names => "Chris")
    add_row(:employee_names => "Shynne")
  end

end

That would set everything exactly like your example, including the title.

Then you just add it to a box, or scrolledwindow:

class GUI

  include GladeGUI

  def initialize()
    load_glade(__FILE__)
    @builder("Scrolledwindow1").add(MyList.new)
    show_window()
  end

end

Go to:

http://www.visualruby.net

慕烟庭风 2024-08-06 15:06:22

基本上,您必须使用 GtkTreeView 并将其“model”属性设置为包含您的数据的 GtkListStore。
GtkTreeView 使用 GtkTreeSelection 类管理选择。 使用 gtk_tree_view_get_selection (或者 ruby​​-gtk 中映射到的任何内容)来获取 GtkTreeSelection。 并将选择模式设置为“多个”。

这是一个 Python 示例。 在 Ruby/Gtk 中应该是类似的。

import pygtk
pygtk.require('2.0')
import gtk
import gobject


w = gtk.Window()
w.connect('destroy', lambda w:gtk.main_quit())

l = gtk.ListStore(gobject.TYPE_STRING)

l.append(('Vinz',))
l.append(('Jhen',))
l.append(('Chris',))
l.append(('Shynne',))

treeview = gtk.TreeView()
treeview.set_model(l)

column = gtk.TreeViewColumn()
cell = gtk.CellRendererText()
column.pack_start(cell)
column.add_attribute(cell,'text',0)
treeview.append_column(column)

treeview.get_selection().set_mode(gtk.SELECTION_MULTIPLE)

def print_selected(treeselection):
    (model,pathlist)=treeselection.get_selected_rows()
    print pathlist

treeview.get_selection().connect('changed',lambda s: print_selected(s))

w.add(treeview)

w.show_all()

gtk.main()

Basically, you have to use GtkTreeView and set its "model" property to a GtkListStore that contains your data.
GtkTreeView manages selection with GtkTreeSelection class. Use gtk_tree_view_get_selection (or whatever it is mapped to in ruby-gtk) to get the GtkTreeSelection. And set the selection mode to "multiple".

Here's an example in Python. In Ruby/Gtk it should be similar.

import pygtk
pygtk.require('2.0')
import gtk
import gobject


w = gtk.Window()
w.connect('destroy', lambda w:gtk.main_quit())

l = gtk.ListStore(gobject.TYPE_STRING)

l.append(('Vinz',))
l.append(('Jhen',))
l.append(('Chris',))
l.append(('Shynne',))

treeview = gtk.TreeView()
treeview.set_model(l)

column = gtk.TreeViewColumn()
cell = gtk.CellRendererText()
column.pack_start(cell)
column.add_attribute(cell,'text',0)
treeview.append_column(column)

treeview.get_selection().set_mode(gtk.SELECTION_MULTIPLE)

def print_selected(treeselection):
    (model,pathlist)=treeselection.get_selected_rows()
    print pathlist

treeview.get_selection().connect('changed',lambda s: print_selected(s))

w.add(treeview)

w.show_all()

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