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. CheckVar1 = IntVar()
  12. CheckVar2 = IntVar()
  13. CheckVar3 = IntVar()
  14. C1 = Checkbutton(a, text = "Music",activebackground="black" , activeforeground="white"\
  15. ,bg="green",bd=10,variable = CheckVar1,\
  16. onvalue = 1, offvalue = 0,command = music).pack()
  17. Label(a, text="").pack()#space
  18. C2 = Checkbutton(a, text = "video",activebackground="black" ,activeforeground="white"\
  19. ,bg="green",bd=10,variable = CheckVar2\
  20. ,onvalue = 1, offvalue = 0,command = video).pack()
  21. Label(a, text="").pack()#space
  22. C3 = Checkbutton(a, text = "photo",activebackground="black" , activeforeground="white"\
  23. ,bg="green",bd=10,variable = CheckVar3,\
  24. onvalue = 1, offvalue = 0,command = photo).pack()
  25. 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. C1 = Checkbutton(a, text ="music",fg="red",bg="green",font="Castellar",height=5,width=5,\
  8. relief="solid",command = music)
  9. C1.pack()
  10. 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. C1 = Checkbutton(a, text ="music",state=DISABLED,justify=RIGHT,padx=10,pady=10\
  8. ,relief="solid",command = music)
  9. C1.pack()
  10. 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!