How would I be able to assign two function to a single button so they execute one function on the first pressing and execute the second function on second pressing?
For example if I made a button (+/-) that should actually display + sign when I click for the first time while clicking second time should display - sign.
use lambda in each button 'command' to pass a button number to the event handler,
Example (using lambda):
import tkinter as tk
class SimpleButtons:
def __init__(self, parent):
self.parent = parent
parent.geometry('80x80+100+100')
# parent.title('lambda button test')
self.example_1()
def event_handler_for_lambda(self, button_no):
if button_no == 1:
print('Button 1 was pressed')
elif button_no == 2:
print('Button 2 was pressed')
def example_1(self):
buttons = []
for btn_no in range(1, 3):
buttons.append(tk.Button(self.parent, text='Button {}'.format(btn_no), padx=10, pady=10,
command=lambda btn_no = btn_no: self.event_handler_for_lambda(btn_no)))
buttons[btn_no-1].grid(row = btn_no-1, column = 0)
def main():
root = tk.Tk()
SimpleButtons(root)
root.mainloop()
if __name__ == '__main__':
main()
You can also use bind and extract widget name in the event handler, but method above is a bit easier
You will have to use some constant to count the number of clicks, but first decide the time limit between clicks, i.e. when is it 2 single clicks and when is it one double click. Using the left button and the middle button would be easier to code.