Check the Check button in Tkinter to allow Entry to be edited

environment: Ubuntu 18.04.5 LTS (Windows Subsystem for Linux) Python 3.8.0 VcXsrv 1.20.5.1

Target

If you check the check box (Check button) in Python Tkinter, the input field (Entry) will be enabled and you can input. If you uncheck it, you will not be able to edit. check.png checkme.png

Method

import tkinter as tk

def change_state():
    if bool_check.get():
        #When checked
        entry.config(state='normal')
    else:
        #When unchecked
        entry.config(state='disabled')

root = tk.Tk()

entry = tk.Entry()
entry.config(state='disabled')  #Initially invalid

bool_check = tk.BooleanVar()
bool_check.set(False)  #No check at first
check = tk.Checkbutton(variable=bool_check, command=change_state)

check.pack()
entry.pack()

root.mainloop()

Define the change_state function.

The variable name can be anything, but for example, assign tk.BooleanVar () to the bool_check variable. Create a checkbox by passing the bool_check variable to the variable argument of tk.Checkbutton () and the change_state function to the command argument.

Checked or not is returned as a Boolean value with bool_check.get (). If unchecked, change_state () will be called.

In change_state (), the state of the created entry is changed depending on whether or not it is checked.

When you want to make two or more

If you can pass bool_check or entry as an argument to change_state (), it seems that you can do the same with two or more input fields. likethis.png However, for example

def change_state(bool_check, entry):
    if bool_check.get():
        entry.config(state='normal')
    else:
        entry.config(state='disabled')

Define the change_state function with arguments, like

tk.Checkbutton(command=change_state(bool_check, entry))

Even if you pass a function with an argument to the command argument of tk.Checkbutton () like, it does not work properly. To work properly, pass a lambda expression to command. That is,

import tkinter as tk

def change_state(bool_check, entry):
    if bool_check.get():
        entry.config(state='normal')
    else:
        entry.config(state='disabled')

root = tk.Tk()

#The first one
entry1 = tk.Entry()
entry1.config(state='disabled')
bool_check1 = tk.BooleanVar()
bool_check1.set(False)
check1 = tk.Checkbutton(text='1', variable=bool_check1,
                        command=lambda: change_state(bool_check1, entry1))
check1.pack()
entry1.pack()

#Second
entry2 = tk.Entry()
entry2.config(state='disabled')
bool_check2 = tk.BooleanVar()
bool_check2.set(False)
check2 = tk.Checkbutton(text='2', variable=bool_check2,
                        command=lambda: change_state(bool_check2, entry2))
check2.pack()
entry2.pack()

root.mainloop()

I feel that this way of writing is better because the change_state function does not depend on global variables.

Easy application

If you uncheck it, the entered character string will be deleted and "invalid" will be displayed.

aaa.png mukou.png
import tkinter as tk

def change_state():
    if bool_check.get():
        #When checked
        entry.config(state='normal')
        entry.delete(0, tk.END)  #After making it normal"Invalid"Delete
    else:
        #When unchecked
        entry.delete(0, tk.END)  #Delete the string from the 0th character to the end
        entry.insert(0, "Invalid")  #0th character of Entry"Invalid"Insert
        entry.config(state='disabled')  #insert and then disable

root = tk.Tk()

entry = tk.Entry()
entry.insert(0, "Invalid")  #0th character of Entry"Invalid"Insert
entry.config(state='disabled')  #insert and then disable

bool_check = tk.BooleanVar()
bool_check.set(False)
check = tk.Checkbutton(variable=bool_check, command=change_state)

check.pack()
entry.pack()

root.mainloop()

It seems that it can be applied, such as giving an initial value.

Recommended Posts

Check the Check button in Tkinter to allow Entry to be edited
How to change the color of just the button pressed in Tkinter
Run the output code with tkinter, saying "A, pretending to be B" in python
Test code to check for broken links in the page
Master the type in Python? (When should type check be done)
Let's create a function to hold down Button in Tkinter
[Python] Solving the import problem due to the difference in entry points
How to check the memory size of a variable in Python
How to check the memory size of a dictionary in Python
LINEbot development, I want to check the operation in the local environment
Have python check if the string can be converted / converted to int
Programming to fight in the world ~ 5-5,5-6
Programming to fight in the world 5-3
Programming to fight in the world-Chapter 4
In the python command python points to python3.8
Check the data summary in CASTable
Cython to try in the shortest
Programming to fight in the world ~ 5-2
Approach commentary for beginners to be in the top 1.5% (0.83732) of Kaggle Titanic_3
How to check local GAE from iPhone browser in the same LAN
Settings that allow IPython Notebook to be accessed from outside the local
Approach commentary for beginners to be in the top 1.5% (0.83732) of Kaggle Titanic_1
Switch the module to be loaded for each execution environment in Python
I made a program to check the size of a file in Python
Approach commentary for beginners to be in the top 1.5% (0.83732) of Kaggle Titanic_2
Check the behavior of destructor in Python
How to check the version of Django
Check if the URL exists in Python
In Jupyter, add IPerl to the kernel.
How to check opencv version in python
Various comments to write in the program
Put Tkinter in Macbook and check operation
A solution to the problem that the Python version in Conda cannot be changed
How to mention a user group in slack notification, how to check the id of the user group
Allow HTML5 <input type = "date / time"> to be used in DatetimeField form in Django
I tried to predict the horses that will be in the top 3 with LightGBM
How to display in the entire window when setting the background image with tkinter
If an exception occurs in the function, it will be transmitted to the caller 2