Reputation: 7187
I have a PyGObject application (Python, GTK+) that performs an action when a button is left clicked.
Right now, that button does nothing when right clicked.
I'd like to make the button perform a different action when right clicked.
Is there a way to do this in PyGObject?
Right now, what I have is:
def setup_gui_objects(self):
"""Set up GUI objects."""
# pylint: disable=cell-var-from-loop
self.buttons = {}
for button_type in ['Run']:
# prints byte strings of hostnames and command names:
# print(self.dict_[b'name'])
toggle_button = Gtk.ToggleButton(label=to_ascii(self.dict_[b'name']))
self.buttons[button_type] = MyButton(toggle_button, self.selected)
self.buttons[button_type].set_name(self.color)
self.buttons[button_type].connect('toggled', lambda widget: self.toggle_selected(button_type, widget))
Thanks.
Upvotes: 0
Views: 81
Reputation: 777
You can use the button_press_event
signal and event.button
to check if left (1
) or right (3
) button was pressed.
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self)
button = Gtk.Button()
button.connect("button_press_event", self.on_button_clicked)
self.add(button)
def on_button_clicked(self, widget, event):
if event.button == 1:
print("Left click")
elif event.button == 3:
print("Right click")
win = MyWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
Upvotes: 1