如何用gtk3在python中创建类似列表的gui?

时间:2015-11-03 21:39:14

标签: python user-interface gtk gtk3

我正在尝试在我的InputPage对象中创建这种类似列表的用户界面gui(没有Glade),这可以在整个gnome3用户界面中看到:

Gtk3 list-like gui

我所拥有的就是这个(请注意,我只想添加按钮+和 - 它们在第一个图像中显示的样式相同,我希望保留列表中的列,就像它一样):< / p>

my gui interface

由于某种原因,按钮比预期的要宽,工具栏不会填充水平空间。 这是我的代码:

class InputPage(Gtk.Box):

def __init__(self):
    Gtk.Box.__init__(self)
    self.grid = Gtk.Grid()
    self.grid.set_column_homogeneous(True)
    self.grid.set_row_homogeneous(True)
    self.add(self.grid)
    #Creating the ListStore model
    self.software_liststore = Gtk.ListStore(str, str, int)
    for software_ref in software_list:
        self.software_liststore.append(list(software_ref))

    #creating the treeview, making it use the filter as a model, and adding the columns
    self.treeview = Gtk.TreeView()
    for i, column_title in enumerate(["Name", "Frequency", "Amplitude"]):
        renderer = Gtk.CellRendererText()
        column = Gtk.TreeViewColumn(column_title, renderer, text=i)
        self.treeview.append_column(column)

    #button.connect("clicked", self.on_selection_button_clicked)

    #setting up the layout, putting the treeview in a scrollwindow, and the buttons in a row
    self.scrollable_treelist = Gtk.ScrolledWindow()
    self.scrollable_treelist.set_vexpand(True)
    self.grid.attach(self.scrollable_treelist, 0, 0, 10, 7)

    #Toolbar
    list_add = Gtk.Button()
    list_add.add(Gtk.Image(icon_name='list-add-symbolic', visible=True))
    list_insert_object = Gtk.Button()
    list_insert_object.add(Gtk.Image(icon_name='insert-object-symbolic', visible=True))
    list_remove = Gtk.Button()
    list_remove.add(Gtk.Image(icon_name='list-remove-symbolic', visible=True))
    self.toolbar = Gtk.ButtonBox(spacing=5)
    self.toolbar.get_style_context().add_class('inline-toolbar')
    self.toolbar.add(list_add)
    self.toolbar.add(list_remove)
    self.toolbar.add(list_insert_object)
    self.toolbar.set_hexpand(True)
    self.grid.attach(self.toolbar, 0,7,4,1)
    #self.grid.attach_next_to(self.toolbar, self.scrollable_treelist, Gtk.PositionType.BOTTOM, 1, 1)
    # for i, button in enumerate(self.buttons[1:]):
        #self.grid.attach_next_to(button, self.buttons[i], Gtk.PositionType.RIGHT, 1, 1)
    self.scrollable_treelist.add(self.treeview)


    self.show_all()

1 个答案:

答案 0 :(得分:2)

您在第一个屏幕截图底部看到的是带有inline-toolbar样式类的标准GtkToolbar。

GtkToolbar不一定需要位于窗口的顶部。您可以将它们放在任何位置,就像常规小部件一样。所有小部件都有一个样式上下文,用于定义小部件的外观;每个样式上下文都应用了一个或多个类。这些类是CSS类;样式化小部件就像在HTML中说class="classname"一样。

以下是一种可能的方法:

  1. 创建一个没有间距的垂直GtkBox。

  2. 将GtkTreeView作为GtkBox的第一个孩子。

  3. 创建一个GtkToolbar并将其作为GtkBox的第二个子项。

  4. 在GtkToolbar上调用get_style_context()方法。

  5. 调用样式上下文的add_class()方法,传递字符串"inline-toolbar"。 (我相信这有一个象征性的常数;我不知道这在Python中是什么。)

  6. 将您的按钮重新创建为GtkToolButtons(非常规GtkButtons!),它们是GtkToolbar的子项。

  7. 如果这不会导致按钮看起来连接,您可以以相同的方式将"linked"类添加到工具栏。

    祝你好运!