Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use tkinter to bind operators like +-*/?

i want to use bind in tkinter to detect if +-*/ is pressed. Is this possible? i’ve checked a lot on the internet and here is the code,i want to know what should i filed in the ???

import tkinter as tk


class Application(tk.Tk):
    def __init__(self):
        super(self.__class__, self).__init__()
        self.title('test')
        self.geometry('300x500')
        self.bind('???', lambda event: self.plus)
        self.bind('???', lambda event: self.subtraction)
        self.bind('???', lambda event: self.multiplication)
        self.bind('???', lambda event: self.division)

    def plus(self):
        print('+ is pressed')

    def subtraction(self):
        print('- is pressed')

    def multiplication(self):
        print('* is pressed')

    def division(self):
        print('/ is pressed')


def main():
    app = Application()
    app.mainloop()


if __name__ == '__main__':
    main()

Thank you.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can bind to the actual characters:

self.bind('+', lambda event: self.plus())
self.bind('-', lambda event: self.subtraction())
self.bind('*', lambda event: self.multiplication())
self.bind('/', lambda event: self.division())

You can also use the following keysyms:

self.bind('<plus>', lambda event: self.plus())
self.bind('<minus>', lambda event: self.subtraction())
self.bind('<asterisk>', lambda event: self.multiplication())
self.bind('<slash>', lambda event: self.division())

For a list of all keysyms see keysyms in the canonical tcl/tk documentation.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading