-
-
Save AnthoniG/72bd4904031713f777b6612f7be23e71 to your computer and use it in GitHub Desktop.
How to programatically add new menu items to menu item in PyGObject.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Hierarchy is: | |
- GtkMenuBar | |
- GtkMenuItem | |
- GtkMenu | |
- GtkMenuItem | |
- GtkImageMenuItem | |
- GtkCheckMenuItem | |
- GtkRadioMenuItem | |
- GtkSeparatorMenuItem | |
""" | |
from gi.repository import Gtk | |
from os.path import abspath, dirname, join | |
WHERE_AM_I = abspath(dirname(__file__)) | |
class MyApp(object): | |
def __init__(self): | |
""" | |
Build GUI | |
""" | |
# States | |
self.counter = 0 | |
# Build GUI from Glade file | |
self.builder = Gtk.Builder() | |
self.glade_file = join(WHERE_AM_I, 'ui.glade') | |
self.builder.add_from_file(self.glade_file) | |
# Get objects | |
go = self.builder.get_object | |
self.window = go('window') | |
self.menubar = go('menubar') | |
# Connect signals | |
self.builder.connect_signals(self) | |
# Configure interface | |
self.window.connect('delete-event', lambda x, y: Gtk.main_quit()) | |
# Everything is ready | |
self.window.show() | |
def populate_menu(self, num): | |
""" | |
Populate application menu | |
""" | |
content = [ | |
'Path to file 1', | |
'Path to file 2', | |
'Path to file 3', | |
'Path to file 4', | |
] | |
# Create menu | |
menu = Gtk.MenuItem(label='My Menu {}'.format(num)) | |
menu.set_submenu(Gtk.Menu()) | |
for p in content: | |
menu.get_submenu().append(Gtk.MenuItem(label=p)) | |
self.menubar.append(menu) | |
menu.show_all() | |
def _btn_cb(self, widget, data=None): | |
""" | |
Button callback | |
""" | |
self.populate_menu(self.counter) | |
self.counter += 1 | |
print('Created menu #{}'.format(self.counter)) | |
if __name__ == '__main__': | |
gui = MyApp() | |
Gtk.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<interface> | |
<!-- interface-requires gtk+ 3.0 --> | |
<object class="GtkWindow" id="window"> | |
<property name="can_focus">False</property> | |
<child> | |
<object class="GtkBox" id="box"> | |
<property name="visible">True</property> | |
<property name="can_focus">False</property> | |
<property name="orientation">vertical</property> | |
<child> | |
<object class="GtkMenuBar" id="menubar"> | |
<property name="height_request">20</property> | |
<property name="visible">True</property> | |
<property name="can_focus">False</property> | |
</object> | |
<packing> | |
<property name="expand">False</property> | |
<property name="fill">True</property> | |
<property name="position">0</property> | |
</packing> | |
</child> | |
<child> | |
<object class="GtkButton" id="button"> | |
<property name="label" translatable="yes">Click Me</property> | |
<property name="visible">True</property> | |
<property name="can_focus">False</property> | |
<property name="receives_default">True</property> | |
<signal name="clicked" handler="_btn_cb" swapped="no"/> | |
</object> | |
<packing> | |
<property name="expand">True</property> | |
<property name="fill">True</property> | |
<property name="position">1</property> | |
</packing> | |
</child> | |
</object> | |
</child> | |
</object> | |
</interface> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment