In this TKinter article i want to show you How to Create CheckButton in TKinter Python. so first of all let’s talk that What is CheckButton in TKinter.
What is CheckButton in Python TKinter?
CheckButton in Python Tkinter is a GUI widget that allows users to select or deselect a particular option by clicking on it. It presents a binary choice (checked or unchecked) and it is commonly used in forms or settings interfaces where users need to indicate their preferences.
When a CheckButton is clicked, its state toggles between selected (checked) and deselected (unchecked). CheckButtons are useful for providing users with multiple choices where more than one option can be selected simultaneously.
How to Create CheckButton in TKinter Python?
In Tkinter, you can create CheckButtons using Checkbutton widget from the tkinter module. You can customize the appearance and behavior of CheckButtons by specifying different options such as text, variable, command and state.
This is the complete code for creating CheckButton in TKinter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
import tkinter as tk from tkinter import ttk # Create a custom Window class inheriting from tk.Tk class Window(tk.Tk): def __init__(self): # Initialize the window tk.Tk.__init__(self) # Set window title self.title("Codeloop.org - Tkinter CheckButton") # Set minimum size for the window self.minsize(600, 400) # Set window icon self.iconphoto(True, tk.PhotoImage(file='codeloop.png')) # Call method to create CheckButtons self.checkButton() # Method to create CheckButtons def checkButton(self): # Create a disabled CheckButton check1 = ttk.Checkbutton(self, text="Disabled", state="disabled") check1.grid(row=0, column=0, sticky=tk.W) # Create an unchecked CheckButton check2 = ttk.Checkbutton(self, text="Unchecked") check2.grid(row=0, column=1) # Create an enabled CheckButton check3 = ttk.Checkbutton(self, text="Enabled") check3.grid(row=0, column=2) # Create an instance of the Window class and start the Tkinter event loop window = Window() window.mainloop() |
In here we have created a class that extends from tk.TK and we have added our window title, window size and window icon in the constructor of the class, make sure that you have added an icon to your working directory . also we have called our checkButton() method in this class.
1 2 3 4 5 6 7 8 |
class Window(tk.Tk): def __init__(self): tk.Tk.__init__(self) self.title("Tkinter Check Buttons") self.minsize(600,400) self.wm_iconbitmap('icon.ico') self.checkButton() |
This method is for creating of our CheckButton, in TKinter we have three types of CheckButton disabled,unchecked and enabled. first we have created the object of the check button and after that we have set the state for the check buttons, also we need to add these check buttons in our grid layout.
1 2 3 4 5 6 7 8 9 |
def checkButton(self): check1 = ttk.Checkbutton(self, text = "Disabled", state = "disabled") check1.grid(row = 0, column= 0, sticky = tk.W) check2 = ttk.Checkbutton(self, text = "Unchecked" ) check2.grid(row = 0, column = 1) check3 = ttk.Checkbutton(self, text = "Enabled") check3.grid(row = 0, column = 2) |
Run the complete source code and this will be the result
FAQs:
How to make a check button in Tkinter?
For creating a check button in Tkinter, you can use Checkbutton widget from the tkinter module. This is a basic example:
1 2 3 4 5 6 |
import tkinter as tk root = tk.Tk() check_button = tk.Checkbutton(root, text="Check me") check_button.pack() root.mainloop() |
How to get value from checkbox in Tkinter?
For getting the value of a checkbox in Tkinter, you can use get method of the associated variable. This is an example:
1 2 3 4 5 6 7 8 9 10 11 12 |
import tkinter as tk def get_checkbox_value(): print("Checkbox value:", check_var.get()) root = tk.Tk() check_var = tk.IntVar() checkbox = tk.Checkbutton(root, text="Checkbox", variable=check_var) checkbox.pack() button = tk.Button(root, text="Get value", command=get_checkbox_value) button.pack() root.mainloop() |
How do I change the color of a checkbox?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import tkinter as tk from tkinter import ttk root = tk.Tk() # Create style object style = ttk.Style(root) # Configure style to change the foreground style.configure('Custom.TCheckbutton', foreground='blue') # Create Checkbutton with the custom style checkbox = ttk.Checkbutton(root, text='Checkbox', style='Custom.TCheckbutton') checkbox.pack() root.mainloop() |
In this example, we have created a custom style named ‘Custom.TCheckbutton’ and configure it to change the foreground color (text color) to blue. after that we creates a Checkbutton widget using this custom style.
Subscribe and Get Free Video Courses & Articles in your Email