How To Use Tkinter Module|Checkbox With Examples |
How To Use Tkinter Module|Checkbox With Examples
Check button: A Check button is used to create a checkbox in python. It is mainly used for selection purposes.
Syntax of Check button in Python
chk = Checkbutton ( w, option1,option2, ... )Here w is the parent window and the option can be used as key-value pairs.
How To Use Tkinter CheckBox in Python?
from tkinter import *
w = Tk()
w.geometry("300x180")
w['bg']="pink"
Checkbutton(w,text="Apple",width="25",onvalue=1,offvalue=0).place(x=10,y=20)
Checkbutton(w,text="Orange",width="25",onvalue=1,offvalue=0).place(x=10,y=50)
Checkbutton(w,text="Cherry",width="25",onvalue=1,offvalue=0).place(x=10,y=80)
w.mainloop()
*****OUTPUT*****
from tkinter import *
w = Tk()
w.geometry("300x180")
w['bg']="pink"
Checkbutton(w,text="Apple",width="25",onvalue=1,offvalue=0).place(x=10,y=20)
Checkbutton(w,text="Orange",width="25",onvalue=1,offvalue=0).place(x=10,y=50)
Checkbutton(w,text="Cherry",width="25",onvalue=1,offvalue=0).place(x=10,y=80)
w.mainloop()
*****OUTPUT*****
w = Tk()
w.geometry("300x180")
w['bg']="pink"
Checkbutton(w,text="Apple",width="25",onvalue=1,offvalue=0).place(x=10,y=20)
Checkbutton(w,text="Orange",width="25",onvalue=1,offvalue=0).place(x=10,y=50)
Checkbutton(w,text="Cherry",width="25",onvalue=1,offvalue=0).place(x=10,y=80)
w.mainloop()
*****OUTPUT*****
How To Use Selected CheckBox in Python?
from Tkinter import message boxfrom Tkinter import *
w = Tk()
w.geometry("300x180")
w['bg']="pink"
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()
Checkbutton(w,text="Apple",variable=chk1,width="15",onvalue=1,offvalue=0).place(x=10,y=20)
Checkbutton(w,text="Orange",variable=chk2,width="15",onvalue=1,offvalue=0).place(x=10,y=50)
Checkbutton(w,text="Cherry",variable=chk3,width="15",onvalue=1,offvalue=0).place(x=10,y=80)
Button(w,text="CLICK HERE",command=fun).place(x=15,y=110)
w.mainloop()
*****OUTPUT*****
from Tkinter import message box
from Tkinter import *w = Tk()
w.geometry("300x180")
w['bg']="pink"
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()
Checkbutton(w,text="Apple",variable=chk1,width="15",onvalue=1,offvalue=0).place(x=10,y=20)
Checkbutton(w,text="Orange",variable=chk2,width="15",onvalue=1,offvalue=0).place(x=10,y=50)
Checkbutton(w,text="Cherry",variable=chk3,width="15",onvalue=1,offvalue=0).place(x=10,y=80)
Button(w,text="CLICK HERE",command=fun).place(x=15,y=110)
w.mainloop()
*****OUTPUT*****
Tkinter Canvas: A Canvas is used to create the graphical layout on which we can draw rectangles, arc, oval, and we can also place text, widgets, or frames on it.
Syntax Of Canvas in Python.
c = Canvas (w, option1,option2, ... )
Here w is the parent window and the option can be used as key-value pairs.
How To Use Tkinter Canvas Drawing in Python?
Listbox: A Python Tkinter Listbox is used to display the list of items to the user.
Syntax of Listbox in Python
l = Listbox (w, option1,option2, ... )
Here w is the parent window and the option can be used as key-value pairs.
How To Use Tkinter ListBox in Python?
How To Delete Items From List in Python?
from tkinter import *w = Tk()
w['bg']="pink"
w.geometry("300x250")
lbl = Label(w, text="My Favourite Fruits",bg="Orange",width="200").pack()
listbox = Listbox(w)
listbox.insert(1, "Apple")
listbox.insert(2, "Orange")
listbox.insert(3, "Cherry")
listbox.insert(4, "Mango")
btn = Button(w, text="delete",bg="orange" ,command=lambda listbox=listbox: listbox.delete(ANCHOR))
listbox.pack(pady=5)
btn.pack()
w.mainloop()
*****OUTPUT*****
from tkinter import *
w = Tk()w['bg']="pink"
w.geometry("300x250")
lbl = Label(w, text="My Favourite Fruits",bg="Orange",width="200").pack()
listbox = Listbox(w)
listbox.insert(1, "Apple")
listbox.insert(2, "Orange")
listbox.insert(3, "Cherry")
listbox.insert(4, "Mango")
btn = Button(w, text="delete",bg="orange" ,command=lambda listbox=listbox: listbox.delete(ANCHOR))
listbox.pack(pady=5)
btn.pack()
w.mainloop()
*****OUTPUT*****
Syntax of Tkinter Messagebox in Python
messagebox.function_name(title, message [, options])Description in Messagebox
Function_name: It is the name of the appropriate message box function.
Title: It is the title of the message box.
Message: It is the text to be displayed as a message on the message box.
Options: Various options can be used to configure the message dialog box.
Functions used with message box in Python
showinfo(): This is used to show normal messages to the user.
show warning(): This is used to show a warning message to the user.
show error (): This is used to show the error message to the user.
ask the question(): This function asks a question to the user that can be answered in either yes or no.
askokcancel(): It confirms the user's action regarding some application activity.
askyesno (): This function asks the user about some action that can be answered in either yes or no.
askretrycancel (): This method is used to ask the user about doing a particular task again or not.
How To Use Messagebox in Python?
from tkinter import messageboxfrom tkinter import *
w = Tk()
w.geometry("200x250")
w['bg']="pink"
def info():
messagebox.showinfo("Info","Info")
def warning():
messagebox.showwarning("Warning", "Warning")
def error():
messagebox.showerror("Error", "Error")
def askq():
messagebox.askquestion("Question","Are you okay?")
def askyes():
messagebox.askyesno("Application","Do you want to install?")
def askok():
messagebox.askokcancel("Storage", "Allow storage")
def askretry():
messagebox.askretrycancel("Failed", "Try again!")
Button(w,text="ShowInfo",command=info).pack(pady=2)
Button(w,text="ShowWarning",command=warning).pack(pady=2)
Button(w,text="ShowError",command=error).pack(pady=2)
Button(w,text="AskQuestion",command=askq).pack(pady=2)
Button(w,text="AskOkCancel",command=askok).pack(pady=2)
Button(w,text="AskYesNo",command=askyes).pack(pady=2)
Button(w,text="AskRetry",command=askretry).pack(pady=2)
w.mainloop()
*****OUTPUT*****
from tkinter import messagebox
from tkinter import *w = Tk()
w.geometry("200x250")
w['bg']="pink"
def info():
messagebox.showinfo("Info","Info")
def warning():
messagebox.showwarning("Warning", "Warning")
def error():
messagebox.showerror("Error", "Error")
def askq():
messagebox.askquestion("Question","Are you okay?")
def askyes():
messagebox.askyesno("Application","Do you want to install?")
def askok():
messagebox.askokcancel("Storage", "Allow storage")
def askretry():
messagebox.askretrycancel("Failed", "Try again!")
Button(w,text="ShowInfo",command=info).pack(pady=2)
Button(w,text="ShowWarning",command=warning).pack(pady=2)
Button(w,text="ShowError",command=error).pack(pady=2)
Button(w,text="AskQuestion",command=askq).pack(pady=2)
Button(w,text="AskOkCancel",command=askok).pack(pady=2)
Button(w,text="AskYesNo",command=askyes).pack(pady=2)
Button(w,text="AskRetry",command=askretry).pack(pady=2)
w.mainloop()
*****OUTPUT*****
Tkinter Menu button: A Python menu button acts like a drop-down that contains a number of the item list. When the user clicks on the menu button it displays the lists.
Syntax of Tkinter Menu button
m = Menubutton (w, option1,option2, ... )
Here w is the parent window and the option can be used as a key-value pair.
How To Use Menubutton in Python?
from tkinter import *w= Tk()
w.geometry("200x150")
w['bg']="pink"
mb = Menubutton(w, text="Menu Programming",bg="navy",fg="white", relief=GROOVE)
mb.grid()
mb.menu = Menu(mb)
mb["menu"] = mb.menu
mb.menu.add_checkbutton(label="C")
mb.menu.add_checkbutton(label="C++")
mb.menu.add_checkbutton(label="JAVA")
mb.menu.add_checkbutton(label="PYTHON")
mb.pack()
w.mainloop()
*****OUTPUT*****
from tkinter import *
w= Tk()w.geometry("200x150")
w['bg']="pink"
mb = Menubutton(w, text="Menu Programming",bg="navy",fg="white", relief=GROOVE)
mb.grid()
mb.menu = Menu(mb)
mb["menu"] = mb.menu
mb.menu.add_checkbutton(label="C")
mb.menu.add_checkbutton(label="C++")
mb.menu.add_checkbutton(label="JAVA")
mb.menu.add_checkbutton(label="PYTHON")
mb.pack()
w.mainloop()
*****OUTPUT*****
Radiobutton: We can create many radio buttons using Tkinter Radiobutton.It is used for selection purposes. We can select only one option from many options.
Syntax of Radiobutton in Python
r = Radiobutton (w,option1,option2, ... )
Here w is the parent window and the option can be used as key-value pairs.
How To Use Tkinter Radiobutton in Python?
from Tkinter import *w = Tk()
w.geometry("300x180")
w['bg']="pink"
rdb=IntVar()
Label(w,text="Select Gender").pack()
Radiobutton(w, text="Male", variable=rdb, value=1).place(x=50,y=20)
Radiobutton(w, text="Female", variable=rdb, value=2).place(x=50,y=50)
Radiobutton(w,text="Other", variable=rdb, value=3).place(x=50,y=80)
w.mainloop()
*****OUTPUT*****
from Tkinter import *
w = Tk()w.geometry("300x180")
w['bg']="pink"
rdb=IntVar()
Label(w,text="Select Gender").pack()
Radiobutton(w, text="Male", variable=rdb, value=1).place(x=50,y=20)
Radiobutton(w, text="Female", variable=rdb, value=2).place(x=50,y=50)
Radiobutton(w,text="Other", variable=rdb, value=3).place(x=50,y=80)
w.mainloop()
*****OUTPUT*****
How To Selected Radiobutton in Python?
from Tkinter import message boxfrom Tkinter import *
w = Tk()
w.geometry("300x180")
w['bg']="orange"
def myFun():
if (rdb.get() == 1):
messagebox.showinfo("Result", "You selected Male")
if (rdb.get() == 2):
messagebox.showinfo("Result", "You selected Female")
if (rdb.get() == 3):
messagebox.showinfo("Result", "You selected Other")
rdb=IntVar()
Label(w,text="Select Gender").pack()
Radiobutton(w, text="Male", variable=rdb, value=1,command=myFun).place(x=50,y=20)
Radiobutton(w, text="Female", variable=rdb, value=2,command=myFun).place(x=50,y=50)
Radiobutton(w, text="Other", variable=rdb, value=3,command=myFun).place(x=50,y=80)
w.mainloop()
*****OUTPUT*****
from Tkinter import message box
from Tkinter import *w = Tk()
w.geometry("300x180")
w['bg']="orange"
def myFun():
if (rdb.get() == 1):
messagebox.showinfo("Result", "You selected Male")
if (rdb.get() == 2):
messagebox.showinfo("Result", "You selected Female")
if (rdb.get() == 3):
messagebox.showinfo("Result", "You selected Other")
rdb=IntVar()
Label(w,text="Select Gender").pack()
Radiobutton(w, text="Male", variable=rdb, value=1,command=myFun).place(x=50,y=20)
Radiobutton(w, text="Female", variable=rdb, value=2,command=myFun).place(x=50,y=50)
Radiobutton(w, text="Other", variable=rdb, value=3,command=myFun).place(x=50,y=80)
w.mainloop()
*****OUTPUT*****