tkinter CheckBox - The Advantages of Learning Python Programming for Beginners: A Comprehensive Guide

Latest

Dive into the world of Python programming with our comprehensive resources

Music

logo

Tuesday, January 3, 2023

tkinter CheckBox


 

In this article I am getting the text of the selected checkbox widget in python. I ensured that the value selected in the checkbox is displayed on the label.

Before starting the example, let's give a little information about checkbuttons.


In Python, checkbuttons return 1 if they are selected and 0 if they are not. They use the get() method when returning values. In addition, the set() function can be used to change the selected states. For example, we can use variable.set(1) to select an unselected box. In addition, if we assign the values to a dictionary and retrieve them with a for loop, we have the chance to make them all selected or zero thanks to a single loop. We use the variable=IntVar() statement to define the variables we will assign as integers beforehand.

 


 

 

 

 

 

 

 

 

 

Figure 1

 

python_checkbox.py

 

from tkinter import *

 

root =Tk()

root.title("checkbox..bs")

root.geometry("200x220")

 

var1=IntVar()

var2=IntVar()

var3=IntVar()

lblMessage = Label(root, text = "Select item", font=('Arial', 12, 'bold'),bg='beige',fg="purple")

lblMessage.place(x = 50, y = 130)

cbox1 = Checkbutton(root, text = "Python", width=12, font=('Arial', 12, 'bold'),fg="blue", variable=var1,onvalue=1)

cbox1.pack()

cbox2 = Checkbutton(root, text = "C#    ",width=12,font=('Arial', 12, 'bold'),fg="blue", variable=var2,onvalue=2)

cbox2.pack()

cbox3 = Checkbutton(root, text = "C++   ",width=12,font=('Arial', 12, 'bold'),fg="blue", variable=var3,onvalue=3)

cbox3.pack()

 

def click():

 

 print(var1.get())

 cboxSelect = var1.get()

 cbox2Select = var2.get()

 cbox3Select = var3.get()

 

 if cboxSelect == 1 and cbox2Select == 2 and cbox3Select == 3  :

  selection = str("Selected Item: " + "\n" + "Python " + "Val=" + str(var1.get()) + "\n" + "C# " + "Val=" + str(var2.get()) + "\n" + "C++ " + "Val=" + str(var3.get()))

  lblMessage.config(text = selection)

 elif cboxSelect == 1  :

  selection = str("Selected Item: " + "Python " + "Val=" + str(var1.get()))

  lblMessage.config(text = selection)

 elif cbox2Select == 2  :

  selection = str("Selected Item: "  + "C# " + "Val=" + str(var2.get()))

  lblMessage.config(text = selection)

 elif cbox3Select == 3  :

  selection = str("Selected Item: "  + "C++ " + "Val=" + str(var3.get()))

  lblMessage.config(text = selection)

 else:

  selection = str("No Selected Item: " + str(var1.get()))

  lblMessage.config(text = selection)

 

btnClick = Button(root,text="Click here",command=click,width=12,font=('Arial', 12, 'bold'),fg="fuchsia")

btnClick.pack()

 

root.mainloop()

 

I have come to the end of this article. See you in my next article. Happy coding. Bahadir Sahin

No comments:

Post a Comment

Featured post

Python Introduction

What is Python? Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its high-level built ...