Tkinter Checkbutton and Canvas in Python

Hello friends how are you, today in this blog i will teach you what is Checkbutton and Canvas, syntax of Checkbutton and Canvas, how to create Checkbutton and Canvas, how Checkbutton and Canvas work in a very simple way.

Let's start 

Checkbutton

  • Checkbutton is used to create checkbox in python.
  • It is mainly used for selection purpose.
  • Syntax:
    chk = Checkbutton ( master, option1,option2, ... )
    
  • Here master is parent window and option can be used as a key-value pairs.
  • Examples of options are bg,font,width etc.

from tkinter import *
top = Tk()
#window dimension
top.geometry("300x180")
top['bg']="#51E1DC"
#create check boxes
Checkbutton(top,text="Apple",width="15",onvalue=1,offvalue=0).place(x=10,y=20)
Checkbutton(top,text="Orange",width="15",onvalue=1,offvalue=0).place(x=10,y=50)
Checkbutton(top,text="Cherry",width="15",onvalue=1,offvalue=0).place(x=10,y=80)
top.mainloop()
 



from tkinter import  messagebox
from tkinter import *
top = Tk()
#window dimension
top.geometry("300x180")
top['bg']="#51E1DC"
def fun():
    str=""
    if chk1.get()==1:
        str=str+ " Apple "
    if chk2.get()==1:
        str=str+" Orange "
    if chk3.get()==1:
        str = str + " Cherry "
    messagebox.showinfo("Result",str+" selected")
chk1=IntVar()
chk2=IntVar()
chk3=IntVar()
#create check boxes
Checkbutton(top,text="Apple",variable=chk1,width="15",onvalue=1,offvalue=0).place(x=10,y=20)
Checkbutton(top,text="Orange",variable=chk2,width="15",onvalue=1,offvalue=0).place(x=10,y=50)
Checkbutton(top,text="Cherry",variable=chk3,width="15",onvalue=1,offvalue=0).place(x=10,y=80)
Button(top,text="CLICK",command=fun).place(x=15,y=110)
top.mainloop()




 TKinter Canvas

  • Canvas is used to create graphical layout on which we can draw rectangle,arc,oval etc and we can also place text,widgets or frames on it.
  • Syntax:
    c = Canvas ( master, option1,option2, ... )
    
  • Here master is parent window and option can be used as a key-value pairs.
  • Examples of options are bg,font,width,height etc.

from tkinter import *
top = Tk()
top.geometry("200x150")
top['bg']="#51E1DC"
c = Canvas(top, bg="#51E1DC", height="200", width=200)
#create arc
arc = c.create_arc((50, 20, 150, 120), start=315, extent=270, fill="yellow")
#create rectangle
arc=c.create_rectangle((50, 120, 150, 140), fill="red")
c.pack()
top.mainloop()
 



Request:-If you found this post helpful then let me know by your comment and share it with your friend. 

If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all. 

If you have not subscribed my website then please subscribe my website. Try to learn something new and teach something new to other. 

Thanks.😊

Post a Comment

0 Comments