Python Tkinter section

Get the value of a Checkbutton (TK)

Card image
Post by Amina Delali, March 15th, 2022

Description

In this code I create a button that will allow me to get the value of a Checkbutton using a Python Tkinter TK Checkbutton widget,a button, and a label that will be used to display the value of the Checkbutton.

So the command property of the Button will be the function that will actually get the value of the Checkbutton

The Checkbutton can have only 2 values associated with its state: 1 (if checked) and 0 if not checked.



The Code

""" How to create and get the value of a checkbutton"""
from tkinter import TkCheckbuttonButtonLabel
from tkinter import IntVar

main_window Tk()
main_window.geometry ('800x400+250+200')
main_window.title("Checkbutton")

var IntVar()             

labVal Label(main_windowtext=""bg="white"fg="green",  font=("Times",14))
labVal.pack(pady=10)

def get_value():
    """ Get the value of Checkbutton and print it on the Label"""
    labVal["text"]=str(var.get())

chB Checkbutton(main_windowtext="I am a check button"variable varfont=("Times",12))
chB.pack(pady=50)

buttonGV Button(main_windowtext="Get the value of the Check Button",
                  bg="green"fg="white"font=("Times",12), command=get_value)
buttonGV.pack(pady=50)

main_window.mainloop()

The Output

Null






Something to say?

If you want to add something about this post, please feel free to do it by commenting below 🙂.