Python Forum
[Tkinter] button command tkinter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] button command tkinter
Hello Python Users:

The following is a simple Python program. If you press the button with label red it prints "red". If you press the button with label blue it prints "blue".

One thing that frustrates me is that I have to write a function for every button. If I have 20 buttons I need 20 funcitons.

What I would like to do is have the same function for every button. Then using an "IF" statement it would print red if the red button was pressed and blue if the blue button was pressed. This would greatly simplify my code. Any suggestions?

def red():

def blue():

red = Button(root, text="Red button", bg="white", fg="red",relief = "ridge", font = "Helvitica 20", command=red)

blue = Button(root, text="blue button", bg="white", fg="blue", relief = "groove",font = "Helvitica 20",command = blue)

Using functools partial to send a value on a button click
import tkinter as tk
from functools import partial

def button_command(colour):

root = tk.Tk()

red = tk.Button(root, text="Red button", bg="white", fg="red", relief="ridge",
                font="Helvitica 20", command=partial(button_command, 'red'))

blue = tk.Button(root, text="blue button", bg="white", fg="blue", relief="groove",
                 font="Helvitica 20", command=partial(button_command, 'blue'))


Creating the buttons in a loop

import tkinter as tk
from functools import partial

def button_command(colour):

root = tk.Tk()

button_colurs = ('red', 'blue', 'green', 'orange')

for button_colour in button_colurs:
    btn = tk.Button(root, text=f"{button_colour} button".capitalize(),
                    bg="white", fg=button_colour,
                    relief="ridge", font="Helvitica 20",
                    command=partial(button_command, button_colour))

Thank Yoriz

Partial was something I previously thought I should study and now I have studied it. It may take me a while to try to apply it to the code (which is much more complicated than my example) that I am trying to create.
partial and lambda should solve all your command callback needs.
I succeeded in using Partial to select from the menu (see last 2 lines above root.mainloop(). If I select "first color" it prints red and if I select the second color it prints green.

I haven't been successful in capturing "red" or "green" inside a variable outside of the function. Can I do this without using a global variable?

from functools import partial

def mltask (mlvar):
    print (mlvar)
    return mlvar

from tkinter import *
root = Tk()
root.configure(background = 'white')

cbutton = Button(root, text="Let's Start", bg="white", \
                        fg="firebrick", relief = "groove", font = "Helvitica 60")

mymenu = Menu(root)
# Create missing letter easy menu
MLEasyMenu = Menu(mymenu)
mymenu.add_cascade(label = "easy", menu=MLEasyMenu)
MLEasyMenu.add_command(label="firstcolor", command = partial(mltask,'red'))
MLEasyMenu.add_command(label="secondcolor", command= partial(mltask,'green'))

Return on the function mltask won't do anything when called by a GUI event.
The code shown does not give an example of what is meant by capturing "red" or "green" inside a variable outside of the function
It would be a lot easier to help you if you wrapped you code in python tags
# code goes here
As for setting variable values, it would be helpful to know what you are trying to do. If you are trying to make a form for entering data, you may want to use tkinter variables and controls that display their value; such as Entry or Checkbutton, Scale, Listbox, etc... This kind of code has only one function callback to process the controls when an Accept button is pressed. If you are writing something with more interaction, you may need to provide a way to store state information. Instead of using a bunch of variables I suggest you read up on Python Classes. Classes are a great way to organize data and functions and work really well when used with GUI interfaces.

Possibly Related Threads…
Thread Author Replies Views Last Post
  Continue command in python tkinter? MLGpotato 7 365 Mar-27-2021, 04:59 AM
Last Post: deanhystad
  tkinter button image Nick_tkinter 4 303 Mar-04-2021, 11:33 PM
Last Post: deanhystad
  tkinter python button position problem Nick_tkinter 3 516 Jan-31-2021, 05:15 AM
Last Post: deanhystad
  TKinter restarting the mainloop when button pressed zazas321 7 664 Jan-26-2021, 06:38 AM
Last Post: zazas321
  tkinter touchscreen scrolling - button press makes unwanted scrolling nanok66 1 338 Dec-28-2020, 10:00 PM
Last Post: nanok66
  Get name of command button Heyjoe 3 372 Dec-10-2020, 04:30 AM
Last Post: deanhystad
  Tkinter - How can I remove the background borders from ttk.Button? TurboC 4 2,469 Oct-18-2020, 10:58 AM
Last Post: TurboC
  tkinter | Button color text on Click Maryan 2 535 Oct-09-2020, 08:56 PM
Last Post: Maryan
  Class function does not create command button Heyjoe 2 485 Aug-22-2020, 08:06 PM
Last Post: Heyjoe
  [Tkinter] Command button, then more command buttons Heyjoe 4 861 Aug-08-2020, 11:28 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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