I have simple app written in python3 and tkinter module. I want to write my custom widget and need to send my custom event.
Why this sample code below does not work?
#!/usr/bin/env python3
from tkinter import *
class MyWidget(Listbox):
def __init__(self, master, *args, **kwargs):
super().__init__(master, *args, **kwargs)
# ===================
# error: _tkinter.TclError: only one event specification allowed
self.bind('<<ListboxSelect>>', lambda e: self.event_generate('MyEvent'))
# ===================
class App(Tk):
def __init__(self):
super().__init__()
w = MyWidget(self)
w.bind('MyEvent', lambda e: print('It\'s working'))
w.pack()
w.insert(END, 'ddddddd')
if __name__ == '__main__':
app = App()
app.mainloop()
Virtual events need to be surrounded by <<
and >>
. Just replace 'MyEvent'
by '<<MyEvent>>'
and your custom event should work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With