Python Forum
[Tkinter] Button click problem using OOP
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Button click problem using OOP
#5
command = lambda:do_something(x) does not work because lambdas are evaluated when called. IN this example I make three buttons and all are set to call DoSomething and passing the argument I.
import tkinter as tk

buttons = []

def DoSomething(args):
    print(args)

root = tk.Tk()
f = tk.Frame()
f.pack()

for i in range(3):
    b = tk.Button(f, command = lambda : DoSomething(i), text=str(i))
    b.grid(row=0, column=i)
    buttons.append(b)
When I press any button the program prints "2". Why is that?

How about answer this. What is the value of i? In my Python console I type i and it responds "2". I press a button, the button executes the lambda. The lambda calls DoSomething and passes the value of i.

To lock in the "i" argument in the callback you can use a partial function. The partial function arguments are frozen when the partial function is created. You are not actually setting the "command" to DoSomething(i), you are setting it to DoSomething(0) or DoSomething(1). You can do something similar with a lambda.
import tkinter as tk

buttons = []

def DoSomething(args):
    print(args)

root = tk.Tk()
f = tk.Frame()
f.pack()

for i in range(3):
    b = tk.Button(f, command = lambda x = i: DoSomething(x), text=str(i))
    b.grid(row=0, column=i)
    buttons.append(b)
Reply


Messages In This Thread
Button click problem using OOP - by JohnB - Oct-18-2020, 07:41 PM
RE: Button click problem using OOP - by deanhystad - Oct-19-2020, 03:38 AM
RE: Button click problem using OOP - by joe_momma - Oct-19-2020, 04:59 AM
RE: Button click problem using OOP - by JohnB - Oct-20-2020, 11:05 AM
RE: Button click problem using OOP - by deanhystad - Oct-20-2020, 04:30 PM
RE: Button click problem using OOP - by JohnB - Oct-21-2020, 12:43 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter - touchscreen, push the button like click the mouse John64 5 869 Jan-06-2024, 03:45 PM
Last Post: deanhystad
  Figure Gets Larger Every time I click a show button joshuagreineder 2 1,335 Aug-11-2022, 06:25 AM
Last Post: chinky
  problem with radio button crook79 3 3,735 Aug-12-2021, 02:30 PM
Last Post: deanhystad
  [Tkinter] Modify Class on Button Click KDog 4 3,980 May-11-2021, 08:43 PM
Last Post: KDog
  tkinter python button position problem Nick_tkinter 3 3,575 Jan-31-2021, 05:15 AM
Last Post: deanhystad
  tkinter | Button color text on Click Maryan 2 3,394 Oct-09-2020, 08:56 PM
Last Post: Maryan
  Closing window on button click not working kenwatts275 4 3,800 May-03-2020, 01:59 PM
Last Post: deanhystad
  Problem about image and button scotesse 5 2,994 Apr-27-2020, 10:09 AM
Last Post: scotesse
  Problem with Submit button Tkinter Reldaing 2 3,674 Jan-05-2020, 01:58 AM
Last Post: balenaucigasa
  [Tkinter] Checking button click in Tkinter GalaxyCoyote 3 7,382 Oct-20-2019, 03:28 AM
Last Post: GalaxyCoyote

Forum Jump:

User Panel Messages

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