Skip to content

[Question] I am trying to add bind to element, maybe button, but not found any widget to bind, #2318

@jason990420

Description

@jason990420

Type of Issues (Question)

bind() is a method of widget in tkinter and Button used here is in PySimpleGUI. In fact, element and widget are different. To call bind, I need to convert element to widget. Checked source code of Tkinter and PySimpleGUI, it seems that it should be TKButton, but failed for widget TKButton is None. So I checked instance of element Button, got result as below. It seems no any widget. How I can do ?

>>> p = button.__dict__
>>> print(*p.items(), sep='\n')

('AutoSizeButton', None)
('BType', 7)
('FileTypes', (('ALL Files', '*.*'),))
('Widget', None)
('TKButton', None)
('Target', (None, None))
('ButtonText', 'Test')
('ButtonColor', ('white', '#517239'))
('ImageFilename', None)
('ImageData', None)
('ImageSize', (None, None))
('ImageSubsample', None)
('UserData', None)
('BorderWidth', 1)
('BindReturnKey', False)
('Focus', False)
('TKCal', None)
('CalendarCloseWhenChosen', None)
('DefaultDate_M_D_Y', (None, None, None))
('InitialFolder', None)
('Disabled', False)
('ChangeSubmits', False)
('Size', (None, None))
('Type', 'button')
('AutoSizeText', None)
('Pad', None)
('Font', None)
('TKStringVar', None)
('TKIntVar', None)
('TKText', None)
('TKEntry', None)
('TKImage', None)
('ParentForm', None)
('ParentContainer', <PySimpleGUI.PySimpleGUI.Window object at 0x00000000017C0EB8>)
('TextInputDefault', None)
('Position', (0, 0))
('BackgroundColor', '#82a459')
('TextColor', 'black')
('Key', 'Test')
('Tooltip', None)
('TooltipObject', None)
('Visible', True)
('TKRightClickMenu', None)
('Tearoff', False)
('ParentRowFrame', None)
('metadata', None)

Operating System

Win10

Python version

Python 3.7.2

PySimpleGUI Port and Version

PySimpleGUI 4.6.0

Your Experience Levels In Months or Years

_2 yrs Python programming experience
>20 yrs
Programming experience overall
_tkinter only Have used another Python GUI Framework (tkiner, Qt, etc) previously (yes/no is fine)?

You have completed these steps:

  • Read instructions on how to file an Issue
  • Searched through main docs http://www.PySimpleGUI.org for your problem
  • Searched through the readme for your specific port if not PySimpleGUI (Qt, WX, Remi)
  • Looked for Demo Programs that are similar to your goal http://www.PySimpleGUI.com
  • Note that there are also Demo Programs under each port on GitHub
  • Run your program outside of your debugger (from a command line)
  • Searched through Issues (open and closed) to see if already reported

Code or partial code causing the problem

import PySimpleGUI as sg

def right_click(self, event):
    # don't care here, maybe it still not working
    self.event_generate('<Button-3>', x=event.x, y=event.y)
    return 'break'

layout = [[sg.Button('Test', key='BTTN')]]
window = sg.Window('Test', layout=layout)

window['BTTN'].Widget.bind('<Button-3>', right_click)

while True:

    event, values = window.read()
    print(event, values)

    if event == None:
        break

window.close()
Traceback (most recent call last):
  File "<string>", line 179, in run_nodebug
  File "D:\Project\Minesweeper\test.py", line 11, in <module>
    window['BTTN'].Widget.bind('<Button-3>', right_click)
AttributeError: 'NoneType' object has no attribute 'bind'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions