Python Forum
[Tkinter] assigning two function for a single button
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] assigning two function for a single button
#1
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.
Reply
#2
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
Reply
#3
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.
Reply
#4
For identifying single/double click see: https://python-forum.io/Thread-wxpython-...ght=Double
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Creating a function interrupt button tkinter AnotherSam 2 5,522 Oct-07-2021, 02:56 PM
Last Post: AnotherSam
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 5,006 Oct-01-2021, 05:00 PM
Last Post: Yoriz
  Class function does not create command button Heyjoe 2 2,271 Aug-22-2020, 08:06 PM
Last Post: Heyjoe
  Tkinter:Unable to bind and unbind function with a button shallanq 2 5,027 Mar-28-2020, 02:05 AM
Last Post: joe_momma
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 4,997 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [Tkinter] Button won't execute function TheLegendOfPanda 2 3,068 Jul-05-2019, 07:41 PM
Last Post: TheLegendOfPanda
  [Tkinter] How make a button perform a function after the user inputs numbers Zephyrforce 1 2,433 May-22-2019, 05:43 PM
Last Post: woooee
  [Kivy] Chagne a button's function after its first pressed TheStraying11 2 5,068 Feb-17-2019, 06:16 PM
Last Post: Yoriz
  Unable to return value from callback function of a button in Python Tkinter nilaybnrj 4 20,781 Aug-05-2018, 11:01 PM
Last Post: woooee
  [Tkinter] loop function when called from tkinter button click WantedStarling 5 8,969 Jul-13-2018, 06:12 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020