Python Tkinter: Color changing grid of buttons?

2024/10/7 12:28:31

I understand that you can make a button that can do some actions when clicked with Tkinter, but how can I just make a button that turns from one color to another when clicked? Then, from that, how do I replicate that button to make a grid of them? I would also settle for a grid of buttons that just change from one character to another.

Answer
import Tkintercolor="red"
default_color="white"def main(n=10):window = Tkinter.Tk()last_clicked = [None]for x in range(n):for y in range(n):b = Tkinter.Button(window, bg=default_color, activebackground=default_color)b.grid(column=x, row=y)# creating the callback with "b" as the default parameter bellow "freezes" its value pointing# to the button created in each run of the loop.b["command"] = lambda b=b: click(b, last_clicked)return windowdef click(button, last_clicked):if last_clicked[0]:last_clicked[0]["bg"] = default_colorlast_clicked[0]["activebackground"] = default_colorbutton["bg"] = colorbutton["activebackground"] = colorlast_clicked[0] = buttonw = main()
Tkinter.mainloop()
https://en.xdnf.cn/q/118820.html

Related Q&A

Writing a function that checks prime numbers

def primecheck(num): if num > 1: for i in range(2, num): if (num % i) == 0: return False breakelse: return TrueIm trying to make a function that checks if an input is prime or not. This code does …

Getting error code 1 while installing geopandas with pip

This is the error I get when trying to install geopandas using pip install geopandas. Im using Python 3.7.Collecting geopandasUsing cached https://files.pythonhosted.org/packages/24/11/d77c157c16909bd7…

Find if a sorted array of floats contains numbers in a certain range efficiently

I have a sorted numpy array of floats, and I want to know whether this array contains number in a given range. Note that Im not interested in the positions of the number in the array. I only want to k…

Django Operation error: (2026, SSL connection error: SSL_CTX_set_tmp_dh failed)

I can not start my django server after running a statement through manage.py for generating class diagrams from db. And I always get this error but I dont know how to deal with it. OperationalError: (2…

TypeError with module object is not callable

I have a test folder the structure within the folder__init.py__ aa.py test.pyfor aa.pyclass aa:def __init__(self,max):self.max=maxprint max+1def hello(self):print(max)for test.pyimport aa abc = aa(100)…

How to access the GUI output?

Im developing one test bench which runs multiple tests via python gui and prints the output as below.A Passed B Passed C Passed D Passed E PassedButton from gui should be changed to Passed only when A,…

Is it possible to have an api call another api, having them both in same application?

I have a python application running on my localhost:3978. Is it possible to make an api call to http://localhost:3978/api/users from http://localhost:3978/api/accounts? @routes.get("/api/accounts…

Find word near other word, within N# of words

I need an enumerating regex function that identifies instances in a string when Word 1 is within N# words of Word 2 For example, here is my dataframe and objective: Pandas Dataframe Input data = [[ABC…

Create new files, dont overwrite existing files, in python

Im writing to a file in three functions and im trying not overwrite the file. I want every time i run the code i generate a new filewith open("atx.csv", w)as output:writer = csv.writer(output…

Python List comprehension execution order [duplicate]

This question already has answers here:Understanding nested list comprehension [duplicate](2 answers)Closed 4 years ago.matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] squared = [[x**2 for x in row] for row…