Python Tkinter program for Checkbutton with Button event handling


In this program, you will learn how to create a Tkinter Checkbutton with Button event handling in Python.


self.chkbtn1 = Checkbutton(tk, text="C", variable=self.checkvar1).place(x=50, y=50)

Example: How to create a Tkinter Checkbutton with Button event handling in Python

from tkinter import *
from tkinter import messagebox


class Test:
    def __init__(self, tk):
        self.checkvar1 = IntVar()
        self.checkvar2 = IntVar()
        self.checkvar3 = IntVar()
        self.checkvar4 = IntVar()

        self.chkbtn1 = Checkbutton(tk, text="C", variable=self.checkvar1).place(x=50, y=50)
        self.chkbtn2 = Checkbutton(tk, text="C++", variable=self.checkvar2).place(x=50, y=80)
        self.chkbtn3 = Checkbutton(tk, text="Python", variable=self.checkvar3).place(x=50, y=110)
        self.chkbtn4 = Checkbutton(tk, text="Java", variable=self.checkvar4).place(x=50, y=140)
        self.btn1 = Button(tk, text="Submit", command=self.submit).place(x=50, y=250)

    def submit(self):
        text1 = text2 = text3 = text4 = ""
        if self.checkvar1.get():
            text1 = "C"
        if self.checkvar2.get():
            text2 = "C++"
        if self.checkvar3.get():
            text3 = "Python"
        if self.checkvar4.get():
            text4 = "Java"
        msg = text1 + " " + text2 + " " + text3 + " " + text4
        messagebox.showinfo("Hello", msg)


tk = Tk()
tk.geometry("600x500")
tk.title("Xiith.com")
myTest = Test(tk)
tk.mainloop()

Output:

tkinter checkbutton event