class WidgetsDemo:
def __init__(self):
window=Tk()
window.title ("Widget Demo")
self.button = [[0 for col in range(5)] for row in range(8)]
for self.i in range(0,8):
for self.j in range(0,5):
self.button[self.i][self.j]=Button(window,bg="white",command=self.colorbox)
self.button[self.i][self.j].grid(row = self.i, column = self.j, sticky = W)
def colorbox(self):
self.button[self.i][self.j].configure(bg="red")
print(self.i,self.j)j)
WidgetsDemo()
I can't make it change color when each button is pressed. What should I do? I don't know if I should just make several variables and make them react when pressed.
python tkinter button
When you hand over the index to pressed, self.x, self.Don't turn it over with y, but use Lambda.
from tkinter import *
class Application(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.buttons = []
self.pack()
self.CreateWidgets()
def CreateWidgets(self):
for i in range(0, 2):
btns = []
for j in range(0, 2):
btns.append(Button(self, text="0", bg="white", command= lambda indexI=i, indexJ=j: self.pressed(indexI,indexJ)))
btns[j].grid(row=i, column=j, sticky=W)
btns[j].pack()
self.buttons.append(btns)
def pressed(self, indexI, indexJ):
try:
self.buttons[indexI][indexJ].configure(bg="black")
print(indexI, indexJ)
except:
pass
root = Tk()
app = Application(master=None)
app.mainloop()
572 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
915 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
581 PHP ssh2_scp_send fails to send files as intended
618 Uncaught (inpromise) Error on Electron: An object could not be cloned
© 2024 OneMinuteCode. All rights reserved.