environment: Ubuntu 18.04.5 LTS (Windows Subsystem for Linux) Python 3.8.0 VcXsrv 1.20.5.1
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.
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.
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. 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.
If you uncheck it, the entered character string will be deleted and "invalid" will be displayed.
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