Search code examples
pythonbuttonpygtkpygobject

How to disable 'button focus' in several buttons in PyGTK?


I'd would like disable the focus in all the buttons of my window. I can disable one button with widget.set_can_focus(False) but I'd like to know the canonical way to apply this feature to all buttons in my window.

FYI, I am using HBox and VBox containers.


Solution

  • I would iterate through a list of the buttons and then use "duck typing" to disable their focus.

    For example:

    button_widgets = [button1, button2, ..., buttonN]
    
    for button in button_widgets:
        button.set_can_focus(False)
    

    UPDATE:

    how to loop through all elements in a HBox or VBox to find buttons:

    If you have the name of the HBoxes or VBoxes at the lowest layers containing the buttons you can simply loop through them, check for buttons and then add them to a list. Here's an example in which I print out all buttons in an HBox:

    import gtk
    import pygtk
    hbox = gtk.HBox()
    button1 = gtk.Button('1')
    button2 = gtk.Button('2')
    hbox.add(button1)
    hbox.add(button2)
    for i in hbox:
        if type(i) == gtk.Button: print i
    

    Outputs:

    <gtk.Button object at 0x1909320 (GtkButton at 0x171e8e0)>
    <gtk.Button object at 0x19093c0 (GtkButton at 0x171e9a0)>