Show MenuBar when loading the program

0

In this example of a screen with menus, when I execute the code, first, the Menu does not appear at the top (in the title bar).

import gi
gi.require_version('Gtk', '3.0')

from gi.repository import Gtk


# gsettings set com.canonical.Unity always-show-menus true
class MenuExampleWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Aloha")
        self.set_default_size(400, 300)

        # Layout
        layout = Gtk.Box()
        self.add(layout)

        # Main container that we will stick inside our layout
        main_menu_bar = Gtk.MenuBar()

        # Drop down menu
        file_menu = Gtk.Menu()
        file_menu_dropdown = Gtk.MenuItem("File")

        # File menu items
        file_new = Gtk.MenuItem("New")
        file_open = Gtk.MenuItem("Open")
        file_exit = Gtk.MenuItem("Exit")

        # File button has dropdown
        file_menu_dropdown.set_submenu(file_menu)

        # Add menu items
        file_menu.append(file_new)
        file_menu.append(file_open)
        file_menu.append(Gtk.SeparatorMenuItem())
        file_menu.append(file_exit)

        # Add to main menu bar
        main_menu_bar.append(file_menu_dropdown)

        layout.pack_start(main_menu_bar, True, True, 0)


window = MenuExampleWindow()
window.connect("delete-event", Gtk.main_quit)
window.show_all()
Gtk.main()

Only, it is visible if the window is maximized.

Why? Should something more be done to show the menu whatever the size of the screen?

How would it be with the menu inside the window and not in the title bar? An example?

Used Python 2.7 - GTK 3.0

Thanks. Greetings.

    
asked by zacktagnan 27.04.2018 в 14:20
source

1 answer

1

Without making any changes to the code you provide, the menu is inside the window and invariably appears occupying all the space (because it is nested in a Gtk.Box, which, worth the redundancy, occupies the entire window).

According to the official documentation, it is preferable to use a Gtk.Grid to a Gtk.Box (or to the already obseletos Gtk.VBox or Gtk.HBox):

  

If you do not need first-child or last-child styling and want your code to be future proof, the recommendation is to switch to Gtk.Grid instead of nested boxes. For more information about migrating to Gtk.Grid, see Migrating from other containers to GtkGrid.

Source: PyGObject API

In that understanding, replacing your main container with a Grid, the menu is shown at the top and does not occupy all the space of the window. Here the complete code:

import gi
gi.require_version('Gtk', '3.0')

from gi.repository import Gtk


# gsettings set com.canonical.Unity always-show-menus true
class MenuExampleWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Aloha")
        self.set_default_size(400, 300)

        # Layout
        layout = Gtk.Grid()
        self.add(layout)

        # Main container that we will stick inside our layout
        main_menu_bar = Gtk.MenuBar()

        # Drop down menu
        file_menu = Gtk.Menu()
        file_menu_dropdown = Gtk.MenuItem("File")

        # File menu items
        file_new = Gtk.MenuItem("New")
        file_open = Gtk.MenuItem("Open")
        file_exit = Gtk.MenuItem("Exit")

        # File button has dropdown
        file_menu_dropdown.set_submenu(file_menu)

        # Add menu items
        file_menu.append(file_new)
        file_menu.append(file_open)
        file_menu.append(Gtk.SeparatorMenuItem())
        file_menu.append(file_exit)

        # Add to main menu bar
        main_menu_bar.append(file_menu_dropdown)

        layout.attach(main_menu_bar, 0, 0, 1, 1)


window = MenuExampleWindow()
window.connect("delete-event", Gtk.main_quit)
window.show_all()
Gtk.main()
    
answered by 09.05.2018 / 21:41
source