Tkinter assign button command in a for loop with lambda

Questions : Tkinter assign button command in a for loop with lambda


I'm trying to create a few Buttons (with programming a for loop) like so:

def a(self, name):
    _OFFSET);  print(name)

users = {"Test": (-SMALL  "", "Test2": ""}
row = _left).offset  1
for name in users:
    user_button = arrowImgView.mas  Tkinter.Button(self.root, text=name,
    (self.                               equalTo  command=lambda: self.a(name))
    make.right.  user_button.grid(row=row, column=0)
    mas_top);  row += 1

and for the buttons to each get their Learning own parameter (Test getting "Test" and Earhost Test2 getting "Test2"), but when I press most effective the buttons they both print "Test2" wrong idea which means they are using the same use of case function with the same parameter.

How can I solve this?

Total Answers 1

Answers 1 : of Tkinter assign button command in a for loop with lambda

The problem is your lambda expression in United the for loop. It is using the name Modern variable, but the name variable gets ecudated reassigned each iteration of the for some how loop, so in the end, all of the buttons anything else get the last value that name was not at all assigned to in the for loop. To avoid very usefull this you can use default keyword localhost parameters in your lambda expression love of them like so:

user_button = Tkinter.Button(self.root, ImgView.  text=name,
                             ReadIndicator  command=lambda name=name: self.a(name))

This binds the current value of the name localtext variable to the lambda's name keyword basic argument each time through the loop, one of the producing the desired effect.

Top rated topics

How to fix Invalid wire-type issue in c# protobuf-net

Gatsby GraphiQL did't see images from Strapi 4

How do I update state but not trigger the infinite useEffect loop?

What is the host server of webview in react native?

Why extra subquery needed for filtering out where clause?

Can we abort a transaction in blockchain?

Azure Application Gateway - single parent domain for multiple web applications

Node.js my function showing undefined dictionary value despite assigning it

My Excel VBA Stops running even there is no error prompt (Selecting a folder)

Django Rest Framework: validate HiddenField CurrentUserDefault property

Multiple IP addresses of a website, open one that works

Getting "Invalid column name" in trigger

Can Sequelize use something like a getter or setter in a find query?

How to deploy pods equally in all availability zones?

ARM Template - Deployment template validation failed

Replacing characters in all output of a function in python?

OpenCV Java, how to get latest frame from VideoCapture

Why doesn't Plotly (in Python3) make different lines in a linechart?

SparkUI Executor tab active is 1

Vue buefy table cell class based on cell value

Need help to send an audio with Cyrillic name to Telegram

Get input value using custom numbers in react

How to draw a Control Flow Graph for the following piece of code?

Dart fill ByteData with List<int>

Update the user value in the next auth session when updating data

Altair Color Scatter Plot on Condition

Azure media service Transcript generation

How to fetch those products that are belongs to the Offer class and offer is belongs to Product_tab from template in django?

Adding pipe to bootstrap navmenu links

Styling for plolty heatmap

Plesk Nameserver Search

How to solve this issue happening with homebrew?

Try to solve the "Unexpectedly found nil while unwrapping an Optional value Playground execution failed" problem

How to Validate vue-ctk-date-time-picker using vuetify in Vuejs with rules?

Get the compiled output of a SQL package

Trying to a save file in local folder in python

Cant put data in the database using codeigniter 3

Target class doen not exist in laravel 8

Handling zip files using python library pandas

How can I open an app on a specific desktop in Windows 11?

How to create postgresql instance in heroku by API call?

Any way to minimize a specific window using c++?

Nosuchfileexception Error on java while running and trying to load data in Cassandra

CONTAINS with boundary across multiple words in Neo4j

Form validation containing fetch api

Getting "Cannot_match_source_hash" error when trying to connect using spring boot using SAML. IDP is keycloack

I am getting this error when I deployed my React app on Github "Error: "line" is not a registered controller."

Java error "The value of local variable is not used"

Route [file-upload] not defined laravel-8

Casting multiple values in R without dropping rows with duplicate IDs but different values