Re: Programatically activate menu like a mouse click




 

Hi John,

Give this a try. It might be close to what you are after.

Eric

#!/usr/bin/python

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
        
class MainWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="Menu Popup")
        self.set_default_size(200, 100)
        self.set_position(Gtk.WindowPosition.CENTER)
      
        self.menubar1 = Gtk.MenuBar()  
        self.menu1 = Gtk.Menu()
        self.menuitem1 = Gtk.MenuItem("Menu") 
        self.menuitem1.set_submenu(self.menu1)
        self.menuitem2 = Gtk.MenuItem("Save")       
        self.menuitem2.connect("activate", self.save_file)      
        self.menuitem3 = Gtk.MenuItem("Print")        
        self.menuitem3.connect("activate", self.print_file)   
        self.menu1.append(self.menuitem2)
        self.menu1.append(self.menuitem3) 
        self.menubar1.append(self.menuitem1)

        self.connect("key-press-event", self.key_pressed)   
        
        self.grid = Gtk.Grid()        
        self.grid.attach(self.menubar1, 0, 0, 1, 1)
        self.add(self.grid)

    def save_file(self, menuitem2):
        print("Save File")

    def print_file(self, menuitem3):
        print("Print File")

    def key_pressed(self, widget, event):
        print("Key Pressed")
        print(str(event.keyval)) 
        self.menu1.popup(None, None, None, None, 0, event.time)
        return False

win = MainWindow()
win.connect("delete-event", Gtk.main_quit) 
win.show_all()
Gtk.main()





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]