[英]How to create list-like gui in python with gtk3?
我正在嘗試在InputPage對象中創建這種類似列表的用戶界面gui(無Glade),可以在整個gnome3用戶界面中看到它:
而我所擁有的就是這個(請注意,我只想添加按鈕+和-並以與第一張圖片中顯示的樣式相同的方式,我希望將列表的列保持原樣):
由於某些原因,按鈕的寬度超出了預期,並且工具欄沒有填充水平空間。 這是我的代碼:
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()
在第一個屏幕截圖的底部看到的是帶有inline-toolbar
樣式類的標准GtkToolbar。
GtkToolbar不一定必須在窗口的頂部。 您可以將它們放置在任何地方,就像常規小部件一樣。 所有小部件都有一個樣式上下文 ,用於定義小部件的外觀。 每個樣式上下文都有一個或多個應用於它的類。 這些類是CSS類。 設置小部件的樣式就像在HTML中說class="classname"
。
這是執行此操作的一種可能方法:
創建一個沒有間隔的垂直GtkBox。
將GtkTreeView放置為GtkBox的第一個孩子。
創建一個GtkToolbar並將其放置為GtkBox的第二個子項。
在GtkToolbar上調用get_style_context()
方法。
調用樣式上下文的add_class()
方法,並傳遞字符串"inline-toolbar"
。 (我相信有一個符號常量;我不知道Python中的含義。)
將您的按鈕重新創建為GtkToolbar的子代GtkToolButtons(不是常規的GtkButtons!)。
如果那沒有導致按鈕看起來已連接,則可以用相同的方式將"linked"
類添加到工具欄。
祝好運!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.