How To Add a Checkbox In Tkinter In Python

Introduction

 
In this article, I will explain how to use a checkbox in tkinter in Python.
 
Definition
 
The Checkbutton keyword is used to display a number of options to a user as toggle buttons. The customer selects one or more options by clicking the button corresponding to each option.
 
Syntax
  1. w = Checkbutton ( a, option) #C is capital letter.  
Parameters
 
a − This represents the parent window of the computer.
 
option − The list of most used options for this widget.
 

Options & Descriptions

  • active background - The active background is used to change Background color when the button is under the cursor.
  • active foreground - The active foreground is used to change the Foreground color when the button is under the cursor.
  • bd - The bd is used to change the border width in pixels. The default is 2.
  • bg - The bg is used to change the normal background color background.
  • command - The command is used to call a function or method when the button is clicked
  • Offvalue - checkbutton is an associated control variable that will be set to 0 when it is cleared (off).
  • OnValue - checkbutton is an associated control variable that will be set to 1 when it is cleared (on).
Program
  1. from tkinter import *      
  2. import tkinter.messagebox      
  3. a= Tk()  
  4. a.geometry("400x400")  
  5. def music():      
  6.    tkinter.messagebox.showinfo(title="music",message="you select music")  
  7. def video():      
  8.    tkinter.messagebox.showinfo(title="video",message="you select video")      
  9. def photo():      
  10.    tkinter.messagebox.showinfo(title="photo",message="you select photo")      
  11.   
  12. CheckVar1 = IntVar()  
  13. CheckVar2 = IntVar()  
  14. CheckVar3 = IntVar()  
  15.   
  16. C1 = Checkbutton(a, text = "Music",activebackground="black" , activeforeground="white"\  
  17.                  ,bg="green",bd=10,variable = CheckVar1,\  
  18.                  onvalue = 1, offvalue = 0,command = music).pack()  
  19.   
  20.   
  21.   
  22. Label(a, text="").pack()#space  
  23.   
  24.   
  25. C2 = Checkbutton(a, text = "video",activebackground="black" ,activeforeground="white"\  
  26.                  ,bg="green",bd=10,variable = CheckVar2\  
  27.                  ,onvalue = 1, offvalue = 0,command = video).pack()  
  28.   
  29.   
  30. Label(a, text="").pack()#space  
  31.   
  32.   
  33. C3 = Checkbutton(a, text = "photo",activebackground="black" , activeforeground="white"\  
  34.                  ,bg="green",bd=10,variable = CheckVar3,\  
  35.                  onvalue = 1, offvalue = 0,command = photo).pack()  
  36.   
  37.   
  38. a.mainloop()    
Output
 
How To Add a Checkbox In The Tkinter In Python
  • fg - The fg is used to change the normal foreground (text) color.
  • font - The font is used to change the text font to be used for the button's label.
  • height - The height is used to change the height of the button in text lines (ftext buttons) or pixels (images).
  • width- The width is used to change the width of the button in letters.
  • highlight color - The highlight color is used to change the color of the focus highlight when the widget has focus.
  • image - Image keyword is used to be displayed on the button.
Program
  1. from tkinter import *      
  2. import tkinter.messagebox      
  3. a= Tk()  
  4. a.geometry("400x400")  
  5. def music():      
  6.    tkinter.messagebox.showinfo(title="music",message="you select music")  
  7.   
  8. C1 = Checkbutton(a, text ="music",fg="red",bg="green",font="Castellar",height=5,width=5,\  
  9.                  relief="solid",command = music)  
  10. C1.pack()  
  11. a.mainloop()     
Output  
 
How To Add a Checkbox In The Tkinter In Python
  • Justify - The justify is used to change the arrangement order LEFT, CENTER, RIGHT.
  • Padx - The padx is used to change the additional padding left and right of the text.
  • Pady - The pady is used to changing the additional padding above and below the text.
  • Relief - Relief is used to specify the type of the border. The values are SUNKEN, RAISED, GROOVE, and RIDGE.
  • State - The state is used to set this option to DISABLED to gray out the button and make it unresponsive.
  • Underline - The underline is used to change the corresponding text character and will be underlined.
Program 
  1. from tkinter import *      
  2. import tkinter.messagebox      
  3. a= Tk()  
  4. a.geometry("400x400")  
  5. def music():      
  6.    tkinter.messagebox.showinfo(title="music",message="you select music")  
  7.   
  8. C1 = Checkbutton(a, text ="music",state=DISABLED,justify=RIGHT,padx=10,pady=10\  
  9.                  ,relief="solid",command = music)  
  10. C1.pack()  
  11.   
  12. a.mainloop()     
Output
 
How To Add a Checkbox In The Tkinter In Python
 

Conclusion

 
In this article, we have seen how to use a checkbox in tkinter in Python. I hope this article was useful to you. Thanks for reading!


Similar Articles