Tkinter Checkbutton Example
Application for simple editing program
Create a dialog box which has 1 Entry text box, 4 function buttons, and 1 check box with the following functions.
-
Entry text box: You can enter text here.
-
Select
function button: you can select the text in Entry. -
Unselect
function button: you can unselect the text in Entry. -
Delete
button: You can delete the text in Entry. -
End
function button: allows you to end the program. -
Read-only
Checkbutton : Make the Entry read-only mode, so that the contents of the Entry cannot be written or changed.
from tkinter import *
# Here are the callback methods
def selAll(): # Select all strings
entry.select_range(0,END)
def deSel(): # Unselect
entry.select_clear()
def clr(): # Delete text
entry.delete(0,END)
def read_only():
print("Calling the read_only function in...",var.get())
if var.get() == True:
entry.config(state=DISABLED) # Set to DISABLED Not available
else:
entry.config(state=NORMAL) # Set to NORMAL
root = Tk()
root.title("apidemos.com") # Title of the window
# The following row=0 to create Entry
entry = Entry(root)
entry.grid(row=0,column=0,columnspan=4,
padx=5,pady=5,sticky=W)
# The following row=1 to create Button
btnSel = Button(root,text="Select",command=selAll)
btnSel.grid(row=1,column=0,padx=5,pady=5,sticky=W)
btnDesel = Button(root,text="Unselect",command=deSel)
btnDesel.grid(row=1,column=1,padx=5,pady=5,sticky=W)
btnClr = Button(root,text="Delete",command=clr)
btnClr.grid(row=1,column=2,padx=5,pady=5,sticky=W)
btnQuit = Button(root,text="End",command=root.destroy)
btnQuit.grid(row=1,column=3,padx=5,pady=5,sticky=W)
# The following row=2 creates Checkboxes
var = BooleanVar()
var.set(False)
# var.set(True)
# read_only()
chkReadOnly = Checkbutton(root,text="Read-only",variable=var,
command=read_only)
chkReadOnly.grid(row=2,column=0)
root.mainloop()
Output: