1

我正在尝试使用 Gtk3 和 Python 创建一个菜单,它的条目像 HTML 表一样对齐。列的值具有不同的长度。我希望菜单看起来像这样:

entry 1      text-of-some-length     1000
entry 2      shorter-text            10
entry 2      shorter-text            100

我认为我可以简单地使用 Gtk.Grid() 将 Gtk.Labels() 添加到它,然后将网格附加到 Gtk.MenuItem()。这是我的尝试

gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GObject
gi.require_version('AppIndicator3', '0.1')
from gi.repository import AppIndicator3 as appindicator
from os.path import expanduser

#init AppIndicator3 in class
self.ind = appindicator.Indicator.new ("todo-txt-indicator", PANEL_ICON, appindicator.IndicatorCategory.OTHER)
self.ind.set_status (appindicator.IndicatorStatus.ACTIVE)

#generate menu for AppIndicator3
menu = Gtk.Menu()

label_1 = Gtk.Label ()
label_1.set_text ('entry 1')

label_2 = Gtk.Label ()
label_2.set_text ('text-of-some-length')

label_3 = Gtk.Label ()
label_3.set_text ('1000')

grid = Gtk.Grid ()
grid.add (label_1)
grid.attach_next_to (label_2, label_1, Gtk.PositionType.RIGHT, 1, 1)
grid.attach_next_to (label_3, label_2, Gtk.PositionType.RIGHT, 1, 1)

menu_item = Gtk.MenuItem ()
menu_item.add (grid)

menu.append (menu_item)

#set menu to AppIndicator3
self.ind.set_menu (menu)

但这不起作用。只有最后一个元素(label_3)被显示(在最左边的位置)。我不明白为什么这不起作用。任何帮助表示赞赏。

4

0 回答 0