Home > database >  How to use tkinter to bind operators like -*/?
How to use tkinter to bind operators like -*/?

Time:08-28

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.

CodePudding user response:

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.

  • Related