PyGtk3, ToggleButton, "toggled-or-untoggled" event - python

i know "toggled-or-untoggled" event is not exist but i need to use event like this. is there a event to do task when button is "toggled" and "untoggled". i don't want use "clicked" event because ToggleButton can be toggled or untoggled without clicked
Thanks
example
def foo(obj):
if obj.get_active():
print("toggled")
else:
print("untoggled")
mybtn = gtk.ToggleButton()
mybtn.connect("toggled-or-untoggled", foo)

According to the docs -
When the state of the button is changed, the “toggled” signal is
emitted.
So, ideally, mybtn.connect("toggled", foo) should work.

Here's a short GTK2+ / PyGTK demo; it should be easy enough to adapt to GTK3, if necessary.
The GUI contains a ToggleButton and a plain Button. The ToggleButton's callback just prints the state of the button whenever it's toggled, either by the user clicking on it or by other code calling its set_active method. The plain Button prints a message when it's clicked, and it also toggles the ToggleButton.
#!/usr/bin/env python2
from __future__ import print_function
import pygtk
pygtk.require('2.0')
import gtk
class Test(object):
def __init__(self):
win = gtk.Window(gtk.WINDOW_TOPLEVEL)
win.connect("destroy", lambda w: gtk.main_quit())
box = gtk.HBox()
box.show()
win.add(box)
self.togglebutton = button = gtk.ToggleButton('toggle')
button.connect("toggled", self.togglebutton_cb)
box.pack_start(button, expand=True, fill=False)
button.show()
button = gtk.Button('plain')
button.connect("clicked", self.button_cb)
box.pack_start(button, expand=True, fill=True)
button.show()
win.show()
gtk.main()
def button_cb(self, widget):
s = "%s button pressed" % widget.get_label()
print(s)
print('Toggling...')
tb = self.togglebutton
state = tb.get_active()
tb.set_active(not state)
def togglebutton_cb(self, widget):
state = widget.get_active()
s = "%s button toggled to %s" % (widget.get_label(), ("off", "on")[state])
print(s)
Test()
typical output
toggle button toggled to on
toggle button toggled to off
plain button pressed
Toggling...
toggle button toggled to on
plain button pressed
Toggling...
toggle button toggled to off
plain button pressed
Toggling...
toggle button toggled to on
toggle button toggled to off

Related

In python code using gtk and glade: How to handle a click on the screen if buttons are also there?

for example if you have a an image, button, and a label. If you don't click the button or anywhere on the screen/window in 10 seconds, then hide the button and the label. If you click the screen/window then it shows the button and label and resets
thanks to Gonzalo Odiard's suggest the issue now is only: How to handle a click on the screen with python gtk if buttons are also there? (how to call custom method "eventListener()" if screen is clicked)
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk, GLib
def button_pressed():
global clicked
clicked = 0
def eventListener():
global clicked
clicked = 0
btn.set_visible(True)
lbl.set_visible(True)#set visible
def hide():
global clicked
if(clicked >=10):
btn.set_visible(False) #set invisible
lbl.set_visible(False)
else:
clicked += 1
GLib.timeout_add_seconds(1, hide)
def callback(window, event):
global countToHide
countToHide = 0
btn.set_visible(True)
lbl.set_visible(True)
width, height= pyautogui.size()
builder = Gtk.Builder()
builder.add_from_file("main.glade")#this gets glade class for GUI
win = builder.get_object("Main_Page")#this sets screen/window to var for display later
global clicked
clicked = 0
btn= builder.get_object("button")#get object button from glade
btn.connect("clicked",button_pressed)#handle events of glade button
lbl= builder.get_object("label")#get Label from glade
#initialize event listener for if screen/window is clicked
#?how to do this
#call method if 10 second go by with out anything being clicked
#this method below
#hide()
GLib.timeout_add_seconds(10, hide)
win.connect('button-press-event', callback)
win.show() #show GUI
Gtk.main()

How to make QPushButton not to be triggered by Enter keyboard key

The code below creates a single dialog window with 5 buttons. Each button is connected to onClick function. If I hit 'Enter' keyboard key one of the buttons is triggered and it onClick function is executed.
How to change the buttons properties so the buttons call for onClick function only when they are clicked and do not respond to Enter keyboard key?
from PyQt4 import QtGui
def onClick():
print 'button clicked'
dialog = QtGui.QDialog()
dialog.setLayout(QtGui.QVBoxLayout())
for i in range(5):
btn = QtGui.QPushButton('Button %03d'%i)
btn.clicked.connect(onClick)
dialog.layout().addWidget(btn)
dialog.show()
Set the default and autoDefault property of the QPushButtons to False. E.g.
btn = QtGui.QPushButton('Button %03d'%i, default=False, autoDefault=False)
What you are observing is the the QDialog's special handling of the enter key to trigger the default 'dialog action' (it is a common gotcha when using QDialog).

How to override default right-click menu in Gtk FileChooserWidget

The gtk FileChooserWidget shows a context menu on right-click with "add to bookmarks", "show hidden files", and "show size column" options. I would like to override this menu with a custom menu.
Creating a menu on right-click from other gtk widgets (window, eventbox) is easy and there are a lot of tutorials out there showing how to do it.
I can't seem to get events from a gtk filechooser widget, however. The "on_button_press_event" callback in the following code never gets called:
#!/usr/bin/env python
import gtk
class file_chooser_test():
def __init__(self):
window = gtk.Window()
window.connect("delete_event", lambda w,e: gtk.main_quit())
chooser = gtk.FileChooserWidget()
chooser.set_size_request(600, 400)
chooser.add_events(gtk.gdk.BUTTON_PRESS_MASK)
chooser.connect("button-press-event", self.on_button_press_event)
window.add(chooser)
window.show_all()
gtk.main()
def on_button_press_event(self, widget, event=None):
print "event is: ", event
if __name__ == "__main__":
test = file_chooser_test()
Can anyone shed any light on how to override the default right-click menu on the GTK FileChooserWidget?
Thanks!

Python GTK - Pass mouse click to window below

I am creating an Ubuntu program that creates a transparent popup window. I would like it to act similarly to the notify-osd popups. When the mouse hovers over the window it fades even more. When the user click the dialog, the event is sent to the window underneath so the user clicks through the popup.
How can I achieve this using Gtk from gi.repository?
Try this:
from gi.repository import Gtk, Gdk
w1 = Gtk.Window()
w2 = Gtk.Window()
def w2_hide(widget, event):
w2.set_opacity(0.25)
def w2_show(widget, event):
w2.set_opacity(0.75)
def w2_click(widget, event):
w2.hide()
w1.set_size_request(600, 400)
w2.set_transient_for(w1)
w2.set_position(Gtk.WindowPosition.CENTER_ON_PARENT)
w1.show()
w2.show()
w2_show(w2, None)
w1.connect("destroy", Gtk.main_quit)
w2.connect("leave-notify-event", w2_hide)
w2.connect("enter-notify-event", w2_show)
w2.add_events(Gdk.EventMask.BUTTON_PRESS_MASK)
w2.connect("button-press-event", w2_click)
Gtk.main()

Putting other widgets in gtk.Menu

I'd like to be able to put a gtk.ProgressBar in my gtk.Menu, but since menus only takes gtk.MenuItems and its subclasses, what I've done instead is take a plain gtk.MenuItem and tried adding my progress bar as a child to that. Since gtk.MenuItem is a subclass of gtk.Bin, it should be able to hold pretty much any widget.
Example:
menu = gtk.Menu()
item = gtk.MenuItem()
button = gtk.ProgressBar()
button.pulse()
button.show()
item.add(button)
item.show()
menu.append(item)
This runs just fine without pygtk complaining at all. However, my progress bar is simply not shown:
If I replace the progressbar with a gtk.Label, it's shown just fine.
Now to my questions:
How do I know which widgets it will take?
How do I trick it into letting me put other widgets in there?
This is a limitation of Ubuntu's Application Indicators, see this question at askubuntu.
Your example code works here(I tested it by modifying a pygtk example that I will paste below).
Maybe its an issue with the rest of your code or your theme?
#!/usr/bin/env python
# example menu.py
import pygtk
pygtk.require('2.0')
import gtk
class MenuExample:
def __init__(self):
# create a new window
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_size_request(200, 100)
window.set_title("GTK Menu Test")
window.connect("delete_event", lambda w,e: gtk.main_quit())
# Init the menu-widget, and remember -- never
# show() the menu widget!!
# This is the menu that holds the menu items, the one that
# will pop up when you click on the "Root Menu" in the app
menu = gtk.Menu()
### MODIFIED PART!! ###
item = gtk.MenuItem()
button = gtk.ProgressBar()
button.pulse()
button.show()
item.add(button)
item.show()
menu.append(item)
#### END MODIFIED PART ####
# This is the root menu, and will be the label
# displayed on the menu bar. There won't be a signal handler attached,
# as it only pops up the rest of the menu when pressed.
root_menu = gtk.MenuItem("Root Menu")
root_menu.show()
# Now we specify that we want our newly created "menu" to be the
# menu for the "root menu"
root_menu.set_submenu(menu)
# A vbox to put a menu and a button in:
vbox = gtk.VBox(False, 0)
window.add(vbox)
vbox.show()
# Create a menu-bar to hold the menus and add it to our main window
menu_bar = gtk.MenuBar()
vbox.pack_start(menu_bar, False, False, 2)
menu_bar.show()
# Create a button to which to attach menu as a popup
button = gtk.Button("press me")
button.connect_object("event", self.button_press, menu)
vbox.pack_end(button, True, True, 2)
button.show()
# And finally we append the menu-item to the menu-bar -- this is the
# "root" menu-item I have been raving about =)
menu_bar.append (root_menu)
# always display the window as the last step so it all splashes on
# the screen at once.
window.show()
# Respond to a button-press by posting a menu passed in as widget.
#
# Note that the "widget" argument is the menu being posted, NOT
# the button that was pressed.
def button_press(self, widget, event):
if event.type == gtk.gdk.BUTTON_PRESS:
widget.popup(None, None, None, event.button, event.time)
# Tell calling code that we have handled this event the buck
# stops here.
return True
# Tell calling code that we have not handled this event pass it on.
return False
# Print a string when a menu item is selected
def menuitem_response(self, widget, string):
print "%s" % string
def main():
gtk.main()
return 0
if __name__ == "__main__":
MenuExample()
main()

Categories

Resources