1.
Button
from tkinter import *
root= Tk()
[Link]("200x100")
b = Button(root,text = "Simple")
[Link]()
[Link]()
2. RadioButton
from tkinter import *
def selection():
selection = "You selected the option " + str([Link]())
[Link](text = selection)
top = Tk()
[Link]("300x150")
radio = IntVar()
lbl = Label(text = "Favourite programming language:")
[Link]()
R1 = Radiobutton(top, text="C", variable=radio, value=1,
command=selection)
[Link]( anchor = W )
R2 = Radiobutton(top, text="C++", variable=radio, value=2,
command=selection)
[Link]( anchor = W )
R3 = Radiobutton(top, text="Java", variable=radio, value=3,
command=selection)
[Link]( anchor = W)
label = Label(top)
[Link]()
[Link]()
3. CheckButton
from tkinter import *
top = Tk()
[Link]("200x200")
checkvar1 = IntVar()
checkvar2 = IntVar()
checkvar3 = IntVar()
chkbtn1 = Checkbutton(top, text = "C", variable = checkvar1, onvalue = 1, offvalue = 0, height =
2, width = 10)
chkbtn2 = Checkbutton(top, text = "C++", variable = checkvar2, onvalue = 1, offvalue = 0, height
= 2, width = 10)
chkbtn3 = Checkbutton(top, text = "Java", variable = checkvar3, onvalue = 1, offvalue = 0,
height = 2, width = 10)
[Link]()
[Link]()
[Link]()
[Link]()
4. Entry
# import tkinter as tk
from tkinter import *
root=Tk()
# setting the windows size
[Link]("600x400")
# declaring string variable
# for storing name and password
name_var=StringVar()
passw_var=StringVar()
# defining a function that will
# get the name and password and
# print them on the screen
def submit():
name=name_var.get()
password=passw_var.get()
print("The name is : " + name)
print("The password is : " + password)
name_var.set("")
passw_var.set("")
# creating a label for
# name using widget Label
name_label = Label(root, text = 'Username', font=('calibre',10, 'bold'))
# creating a entry for input
# name using widget Entry
name_entry = Entry(root,textvariable = name_var, font=('calibre',10,'normal'))
# creating a label for password
passw_label = Label(root, text = 'Password', font = ('calibre',10,'bold'))
# creating a entry for password
passw_entry=Entry(root, textvariable = passw_var, font = ('calibre',10,'normal'), show = '*')
# creating a button using the widget
# Button that will call the submit function
sub_btn=Button(root,text = 'Submit', command = submit)
# placing the label and entry in
# the required position using grid
# method
name_label.grid(row=0,column=0)
name_entry.grid(row=0,column=1)
passw_label.grid(row=1,column=0)
passw_entry.grid(row=1,column=1)
sub_btn.grid(row=2,column=1)
# performing an infinite loop
# for the window to display
[Link]()
5. SpinBox
from tkinter import *
master = Tk()
w = Spinbox(master, from_ = -10, to = 10)
[Link](x=0,y=10)
mainloop()
6. Frames
from tkinter import *
top = Tk()
[Link]("140x100")
frame = Frame(top)
[Link]()
leftframe = Frame(top)
[Link](side = LEFT)
rightframe = Frame(top)
[Link](side = RIGHT)
btn1 = Button(frame, text="Submit", fg="red",activebackground = "red")
[Link](side = LEFT)
btn2 = Button(frame, text="Remove", fg="brown", activebackground = "brown")
[Link](side = RIGHT)
btn3 = Button(rightframe, text="Add", fg="blue", activebackground = "blue")
[Link](side = LEFT)
btn4 = Button(leftframe, text="Modify", fg="black", activebackground = "white")
[Link](side = RIGHT)
[Link]()
7. LabelFrame
from tkinter import *
top = Tk()
[Link]("300x200")
labelframe1 = LabelFrame(top, text="Positive Comments")
[Link](fill="both", expand="yes")
toplabel = Label(labelframe1, text="Place to put the positive comments")
[Link]()
labelframe2 = LabelFrame(top, text = "Negative Comments")
[Link](fill="both", expand = "yes")
bottomlabel = Label(labelframe2,text = "Place to put the negative comments")
[Link]()
[Link]()
8. ListBox
from tkinter import *
# create a root window.
top = Tk()
# create listbox object
listbox = Listbox(top, height = 10,
width = 15,
bg = "grey",
activestyle = 'dotbox',
font = "Helvetica",
fg = "yellow")
# Define the size of the window.
[Link]("300x250")
# Define a label for the list.
label = Label(top, text = " FOOD ITEMS")
# insert elements by their
# index and names.
[Link](1, "Nachos")
[Link](2, "Sandwich")
[Link](3, "Burger")
[Link](4, "Pizza")
[Link](5, "Burrito")
# pack the widgets
[Link]()
[Link]()
# Display untill User
# exits themselves.
[Link]()
9. Adding Images
# Import required libraries
from tkinter import *
from PIL import ImageTk, Image
# Create an instance of tkinter window
win = Tk()
# Define the geometry of the window
[Link]("300x205")
frame = Frame(win, width=600, height=400)
[Link]()
[Link]()
# Create an object of tkinter ImageTk
img=[Link]("C:\\Users\\acer\\Pictures\\Video Projects\\[Link]")
resized_img= [Link]((300,205), [Link])
new_img = [Link](resized_img)
# Create a Label Widget to display the text or Image
label = Label(frame, image = new_img)
[Link]()
[Link]()
10. Canvas
from tkinter import *
root = Tk()
C = Canvas(root, bg="yellow",
height=250, width=300)
line = C.create_line(108, 120,
320, 40,
width="10",
fill="brown")
arc = C.create_arc(180, 150, 80,
210, start=0,
extent=220,
fill="red")
oval = C.create_oval(80, 30, 140,
150,
fill="blue")
[Link]()
mainloop()
11. MessageBox
from tkinter import *
from tkinter import messagebox
root = Tk()
[Link]("300x200")
w = Label(root, text ='MessageBox Tutorial', font = "50")
[Link]()
[Link]("showinfo", "Information")
[Link]("showwarning", "Warning")
[Link]("showerror", "Error")
[Link]("askquestion", "Are you sure?")
[Link]("askokcancel", "Want to continue?")
[Link]("askyesno", "Find the value?")
[Link]("askretrycancel", "Try again?")
[Link]()
12. Binding Function
from tkinter import *
# function to be called when
# keyboard buttons are pressed
def key_press(event):
key = [Link]
print(key, 'is pressed')
# creates tkinter window or root window
root = Tk()
[Link]('200x100')
# here we are binding keyboard
# with the main window
[Link]('<Key>', key_press)
mainloop()
13. ScrollBar
from tkinter import *
top = Tk()
sb = Scrollbar(top)
[Link](side = RIGHT, fill = Y)
mylist = Listbox(top, yscrollcommand = [Link] )
for line in range(30):
[Link](END, "Number " + str(line))
[Link]( side = LEFT )
[Link]( command = [Link] )
mainloop()
14. MenuBar
from tkinter import *
# creating tkinter window
root = Tk()
[Link]('Menu Demonstration')
# Creating Menubar
menubar = Menu(root)
# Adding File Menu and commands
file = Menu(menubar, tearoff = 0)
menubar.add_cascade(label ='File', menu = file)
file.add_command(label ='New File', command = None)
file.add_command(label ='Open...', command = None)
file.add_command(label ='Save', command = None)
file.add_separator()
file.add_command(label ='Exit', command = [Link])
# Adding Edit Menu and commands
edit = Menu(menubar, tearoff = 0)
menubar.add_cascade(label ='Edit', menu = edit)
edit.add_command(label ='Cut', command = None)
edit.add_command(label ='Copy', command = None)
edit.add_command(label ='Paste', command = None)
edit.add_command(label ='Select All', command = None)
edit.add_separator()
edit.add_command(label ='Find...', command = None)
edit.add_command(label ='Find again', command = None)
# Adding Help Menu
help_ = Menu(menubar, tearoff = 0)
menubar.add_cascade(label ='Help', menu = help_)
help_.add_command(label ='Tk Help', command = None)
help_.add_command(label ='Demo', command = None)
help_.add_separator()
help_.add_command(label ='About Tk', command = None)
# display Menu
[Link](menu = menubar)
[Link]()