How to change the color of just the button pressed in Tkinter

example.py


def setnumber():
    column = -1
    row = 0
    root = tk.Tk()
    root.title('numbers')
    root.geometry('470x310')
    for i in range(101):
        if i > 0:
            if i%10 == 1:
                row += 1 
                column = -1
            column += 1
            text=f'{i}'
            btn = tk.Button(root, text=text)
            btn.grid(column=column, row=row)
            btn.config(command=collback(btn))
    root.mainloop()

def collback(btn):
    def nothing():
        btn.config(bg='#008000')
    return nothing

First, I am making buttons from 1 to 100 at setnumber (). The reason why it looks a little confusing is that when I made 10 pieces, I made a line break. Finally, specify collback (btn) as the callback function and pass the variable btn as an argument. In that function, call a function called nothing to change the color. When you do this, only the button pressed will turn green as shown below. Adobe_20200622_150740.jpg

Recommended Posts

How to change the color of just the button pressed in Tkinter
How to change editor color in PyCharm
[Touch Designer] Change the appearance color of Button COMP
How to get the number of digits in Python
[Python] Summary of how to specify the color of the figure
How to change the appearance of unselected Foreign Key fields in Django's ModelForm
How to find the optimal number of clusters in k-means
Check the Check button in Tkinter to allow Entry to be edited
How to change vim color scheme
How to handle multiple versions of CUDA in the same environment
How to determine the existence of a selenium element in Python
How to change the log level of Azure SDK for Python
How to implement Java code in the background of RedHat (LinuxONE)
How to know the internal structure of an object in Python
How to check the memory size of a variable in Python
Feel free to change the label of the legend in Seaborn in python
How to check the memory size of a dictionary in Python
[TensorFlow 2] How to check the contents of Tensor in graph mode
How to get the vertex coordinates of a feature in ArcPy
I summarized how to change the boot parameters of GRUB and GRUB2
Script to change the description of fasta
How to check the version of Django
I want to change the color by clicking the scatter point in matplotlib
How to change multiple columns of csv in Pandas (Unixtime-> Japan Time)
Change the saturation and brightness of color specifications like # ff000 in python 2.5
How to compare if the contents of the objects in scipy.sparse.csr_matrix are the same
How to calculate the volatility of a brand
How to use the C library in Python
How to find the area of the Voronoi diagram
How to keep track of work in Powershell
[Python] How to change the date format (display format)
Change the font size of the legend in df.plot
Summary of how to use MNIST in Python
Change the color of Fabric errors and warnings
Change the decimal point of logging from, to.
How to get the files in the [Python] folder
How to use tkinter with python in pyenv
How to Learn Kaldi with the JUST Corpus
How to pass the execution result of a shell command in a list in Python
How to mention a user group in slack notification, how to check the id of the user group
How to uniquely identify the source of access in the Django Generic Class View
How to count the number of elements in Django and output to a template
In Python, change the behavior of the method depending on how it is called
A memorandum of how to execute the! Sudo magic command in Jupyter Notebook
How to display in the entire window when setting the background image with tkinter
How to find the coefficient of the trendline that passes through the vertices in Python
How to make the font width of jupyter notebook put in pyenv equal width
How to get a list of files in the same directory with python
How to install the deep learning framework Tensorflow 1.0 in the Anaconda environment of Windows
How to retrieve the nth largest value in Python
How to know the port number of the xinetd service
How to get the variable name itself in python
How to run the Ansible module added in Ansible Tower
How to display the modification date of a file in C language up to nanoseconds
How to know the current directory in Python in Blender
How to identify the element with the smallest number of characters in a Python list?
How to visualize the decision tree model of scikit-learn
[Python] Change the text color and background color of a specific keyword in print output
How to display multiple images of galaxies in tiles
How to use the exists clause in Django's queryset
[Blender] How to dynamically set the selection of EnumProperty