Events and Bindings in tkinter does not work in loop

2024/10/11 12:26:55

I am trying to create binding in a loop using tkinter module.

from tkinter import *
class Gui(Frame):def __init__(self, parent):Frame.__init__(self, parent)  self.parent = parentself.initUI()def Arrays(self,rankings):self.panels = {}self.helpLf = LabelFrame(self, text="Array Layout:")self.helpLf.grid(row=10, column=9, columnspan=5, rowspan=8)for rows in range(10):for cols in range(10):x  = str(rows)+"_"+str(cols)self.row = Frame(self.helpLf)self.bat = Button(self.helpLf,text=" ", bg = "white")self.bat.grid(row=10+rows, column=cols)self.panels[x] = self.batself.panels[x].bind('<Button-1>', self.make_lambda())self.panels[x].bind('<Double-1>', self.color_change1)def color_change(self, event):"""Changes the button's color"""self.bat.configure(bg = "green")def color_change1(self, event):self.bat.configure(bg = "red")

There are 10X10= 100 buttons here. the binder works only for the last button. Does anyone know how can I apply the binder for all the buttons?

Answer

You use self.bat in color_change1 but self.bat keeps last button because you ovewrite it in loop. But you keep all buttons in self.panels[x] to have access to any button. So you could use it.

But there is simpler solution - binding use variable event to send some information about event to executed function. For example event.widget give you access to widget which execute function color_change1 so you can use it:

def color_change1(self, event):event.widget.configure(bg = "red")

See "Event Attributes" on page Events and Bindings

https://en.xdnf.cn/q/118324.html

Related Q&A

Python Machine Learning Algorithm to Recognize Known Events

I have two sets of data. These data are logged voltages of two points A and B in a circuit. Voltage A is the main component of the circuit, and B is a sub-circuit. Every positive voltage in B is (1) co…

How can I replace Unicode characters in Python?

Im pulling Twitter data via their API and one of the tweets has a special character (the right apostrophe) and I keep getting an error saying that Python cant map or character map the character. Ive lo…

Filtering Pandas DataFrame using a condition on column values that are numpy arrays

I have a Pandas DataFrame called dt, which has two columns called A and B. The values of column B are numpy arrays; Something like this: index A B 0 a [1,2,3] 1 b [2,3,4] 2 c …

Creation a tridiagonal block matrix in python [duplicate]

This question already has answers here:Block tridiagonal matrix python(9 answers)Closed 6 years ago.How can I create this matrix using python ? Ive already created S , T , X ,W ,Y and Z as well as the…

Python tkinter checkbutton value always equal to 0

I put the checkbutton on the text widget, but everytime I select a checkbutton, the function checkbutton_value is called, and it returns 0.Part of the code is :def callback():file_name=askopenfilename(…

How does derived class arguments work in Python?

I am having difficulty understanding one thing in Python.I have been coding in Python from a very long time but theres is something that just struck me today which i struggle to understandSo the situat…

grouping on tems in a list in python

I have 60 records with a column "skillsList" "("skillsList" is a list of skills) and "IdNo". I want to find out how many "IdNos" have a skill in common.How …

How do I show a suffix to user input in Python?

I want a percentage sign to display after the users enters their number. Thankspercent_tip = float(input(" Please Enter the percent of the tip:")("%"))For example, before the user t…

Discord.py Self Bot using rewrite

Im trying to make a selfbot using discord.py rewrite. Im encountering issues when attempting to create a simple command. Id like my selfbot to respond with "oof" when ">>>test&q…

int to binary python

This question is probably very easy for most of you, but i cant find the answer so far.Im building a network packet generator that goes like this:class PacketHeader(Packet): fields = OrderedDict([(&quo…