0% found this document useful (0 votes)
45 views

Python Tkinter Programs For Students

The document provides examples of using Tkinter to create graphical user interfaces in Python. Tkinter allows creating windows, labels, buttons, text boxes and other common GUI elements. Various layout managers like pack, grid and absolute positioning can be used to arrange GUI elements. Events like button clicks and data binding are also demonstrated.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Python Tkinter Programs For Students

The document provides examples of using Tkinter to create graphical user interfaces in Python. Tkinter allows creating windows, labels, buttons, text boxes and other common GUI elements. Various layout managers like pack, grid and absolute positioning can be used to arrange GUI elements. Events like button clicks and data binding are also demonstrated.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

‭Tkinter‬

‭1.‬ T‭ o check whether its installed –‬


‭Python –m tkinter‬

‭2.‬ O
‭ ne.py‬
‭import tkinter‬
‭top = tkinter.Tk()‬
‭top.mainloop()‬

‭ .‬
3
‭from tkinter import *‬
‭root = Tk()‬
‭root.title("Python GUI Programming")‬
‭root.configure(bg="#aabbcc")‬
‭root.geometry("800x200")‬
‭root.mainloop()‬
‭ .‬
4
‭from tkinter import *‬

‭root = Tk()‬

l‭abel1 = Label(root, text="welcome to", fg="red", bg="yellow")‬


‭label2 = Label( root, text="VES College", relief=RAISED )‬
‭label1.pack()‬
‭label2.pack()‬
‭root.mainloop()‬

‭ .‬
5
‭from tkinter import *‬

‭root = Tk()‬

l‭abel1 = Label(root, text="welcome to", fg="red", bg="yellow")‬


‭label2 = Label( root, text="VES College", relief=RAISED )‬
‭button = Button(root, text="Click me")‬
‭label1.pack()‬
‭label2.pack()‬
‭button.pack()‬
‭root.mainloop()‬

‭ .‬
6
‭import tkinter‬
‭import tkinter.messagebox‬
‭def OnClick():‬
‭tkinter.messagebox.showinfo( "Hello Python", "Hello World")‬
‭top = tkinter.Tk()‬
‭B = tkinter.Button(top, text ="Hello", command = OnClick)‬
‭B.pack()‬
‭top.mainloop()‬

‭ .‬
7
‭import tkinter.messagebox‬
‭from tkinter import *‬

‭def OnClick():‬
‭tkinter.messagebox.showinfo( "Hello Python", "Hello World")‬
‭print("button clicked")‬

t‭ op = Tk()‬
‭B = Button(top, text ="Hello", command=OnClick)‬
‭ .pack()‬
B
‭top.mainloop()‬

‭9. Using Grid‬

‭from tkinter import *‬

‭mainwindow = Tk()‬

l‭abel1 = Label(mainwindow, text="Enter name :")‬


‭entry1 = Entry(mainwindow)‬

l‭abel2 = Label(mainwindow, text="Enter Pass :")‬


‭entry2 = Entry(mainwindow)‬

l‭abel1.grid(row=0,column=0)‬
‭entry1.grid(row=0,column=1)‬
‭label2.grid(row=1,column=0)‬
‭entry2.grid(row=1,column=1)‬

‭mainwindow.mainloop()‬

‭10. Controlling alignments‬

‭from tkinter import *‬

r‭ oot = Tk()‬
‭frame = Frame(root)‬
‭frame.pack()‬

‭ ottomframe = Frame(root)‬
b
‭bottomframe.pack( side = BOTTOM )‬

r‭ edbutton = Button(frame, text="Red", fg="red")‬


‭redbutton.pack( side = LEFT)‬

g‭ reenbutton = Button(frame, text="Brown", fg="brown")‬


‭greenbutton.pack( side = LEFT )‬
‭pack(‬
‭bluebutton = Button(frame, text="Blue", fg="blue")‬
‭bluebutton.pack( side = LEFT )‬

‭ lackbutton = Button(bottomframe, text="Black", fg="black")‬


b
‭blackbutton.pack( side = BOTTOM)‬
‭root.mainloop()‬

‭11. Data Binding‬


‭from tkinter import *‬

‭root = Tk()‬

v‭ ar = StringVar()‬
‭label = Label( root, textvariable=var)‬

v‭ ar.set("Hey!? How are you doing?")‬


‭label.pack()‬
‭root.mainloop()‬

‭12.‬

‭from tkinter import *‬

‭def readvar():‬
‭print(x.get())‬

‭def changevar():‬
‭name=input("Enter a name :")‬
‭x.set(name)‬

t‭ op = Tk()‬
‭x = StringVar()‬
‭x.set("xyz")‬

e‭ ntry = Entry(top, textvariable=x)‬


‭entry.pack()‬

‭ 1 = Button(top, text="Read" , command=readvar)‬


b
‭b1.pack()‬

‭ 2 = Button(top, text="Change", command=changevar)‬


b
‭b2.pack()‬
‭top.mainloop()‬

‭13.‬

‭from tkinter import *‬

‭def callme():‬
‭x = var.get()‬
‭if x == 1 :‬
‭b.config(text="click again" , fg="blue", state="disabled") #disabled or active‬
‭x = x+ 1‬
‭var.set(x)‬

‭root = Tk()‬

‭var = IntVar()‬

‭ = Button(root,text="Here we go..", command=callme)‬


b
‭b.pack(expand="YES", fill="x")‬

l‭abel = Label( root, textvariable=var)‬


‭var.set(1)‬
‭label.pack()‬

‭root.mainloop()‬

‭14. Using radio buttons‬

‭from tkinter import *‬

‭def sel():‬
‭selection = "You selected the option " + str(var.get())‬
‭label.config(text = selection)‬

r‭ oot = Tk()‬
‭var = IntVar()‬
‭var.set(2)‬
‭R1 = Radiobutton(root, text="FYCS", variable=var, value=1,‬
‭command=sel)‬
‭R1.pack( anchor = W )‬

‭R2 = Radiobutton(root, text="SYCS", variable=var, value=2,‬


‭command=sel)‬
‭R2.pack( anchor = E )‬

‭R3 = Radiobutton(root, text="TYCS", variable=var, value=3,‬


‭command=sel)‬
‭R3.pack( anchor = W)‬

l‭abel = Label(root)‬
‭label.pack()‬
‭root.mainloop()‬

‭15. entry‬

‭from tkinter import *‬


‭def sel():‬
‭selection = "You selected the option\n " + str(var.get())‬
‭text.delete("1.0", "end")‬
‭text.insert(END, selection)‬
‭root = Tk()‬
‭var = IntVar()‬
‭R1 = Radiobutton(root, text="Option 1", variable=var, value=1,‬
‭command=sel)‬
‭R1.pack( anchor = W )‬

‭R2 = Radiobutton(root, text="Option 2", variable=var, value=2,‬


‭command=sel)‬
‭R2.pack( anchor = W )‬

‭R3 = Radiobutton(root, text="Option 3", variable=var, value=3,‬


‭command=sel)‬
‭R3.pack( anchor = W)‬

t‭ ext = Text(root, width=30, height=2 )‬


‭text.pack(anchor=W)‬
‭root.mainloop()‬

‭ 6. check boxes‬
1
‭from tkinter import *‬

t‭ op = Tk()‬
‭CheckVar1 = IntVar()‬
‭CheckVar2 = IntVar()‬

‭C1 = Checkbutton(top, text = "Music", variable = CheckVar1, \‬


‭onvalue = 1, offvalue = 0)‬
‭C2 = Checkbutton(top, text = "Video", variable = CheckVar2, \‬
‭onvalue = 1, offvalue = 0)‬
‭C1.pack()‬
‭C2.pack()‬
‭top.mainloop()‬

‭ 7. Adding menus‬
1
‭from tkinter import *‬

r‭ oot = Tk()‬
‭root.title('Menu Demonstration')‬

#‭ Creating Menubar‬
‭menubar = Menu(root)‬

‭# Adding File Menu and commands‬


‭ le = Menu(menubar, tearoff = 0) #tearoff - can't detach menu‬
fi
‭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 = root.destroy)‬

#‭ Adding Help Menu‬


‭help_ = Menu(menubar, tearoff = 0)‬
‭menubar.add_cascade(label ='Help', menu = help_)‬
‭help_.add_command(label ='About Tk', command = None)‬

#‭ display Menu‬
‭root.config(menu = menubar)‬
‭mainloop()‬

‭18. adding combobox‬

i‭mport tkinter as tk‬


‭from tkinter import ttk‬
‭from tkinter.messagebox import showinfo‬

‭root = tk.Tk()‬

#‭ config the root window‬


‭root.geometry('300x200')‬
‭root.resizable(False, False)‬
‭root.title('Combobox Widget')‬

#‭ label‬
‭label = ttk.Label(text="Please select a month:")‬
‭label.pack(fill=tk.X, padx=5, pady=5)‬

#‭ create a combobox‬
‭selected_month = tk.StringVar()‬
‭month_cb = ttk.Combobox(root, textvariable=selected_month)‬

#‭ get first 3 letters of every month name‬


‭month_cb['values'] = ["jan", "feb", "mar", "dec" ]‬

#‭ prevent typing a value‬


‭month_cb['state'] = 'readonly'‬

‭# place the widget‬


‭month_cb.pack(fill=tk.X, padx=5, pady=5)‬

#‭ bind the selected value changes‬


‭def month_changed(event):‬
‭""" handle the month changed event """‬
‭showinfo(‬
‭title='Result',‬
‭message='You selected ' + selected_month.get()‬
‭)‬

‭month_cb.bind('<<ComboboxSelected>>', month_changed)‬

‭root.mainloop()‬

‭19. listbox‬

i‭mport tkinter as tk‬


‭from tkinter import ttk‬
‭from tkinter.messagebox import showinfo‬

#‭ create the root window‬


‭root = tk.Tk()‬
‭root.title('Listbox')‬

#‭ create a list box‬


‭langs = ('Java', 'C#', 'C', 'C++', 'Python',‬
‭'Go', 'JavaScript', 'PHP', 'Swift')‬

‭var = tk.Variable(value=langs)‬

‭listbox = tk.Listbox(‬
‭root,‬
‭listvariable=var,‬
‭height=6,‬
‭selectmode=tk.MULTIPLE‬
‭)‬

‭listbox.pack(expand=True, fill=tk.BOTH)‬

‭def items_selected(event):‬
‭# get all selected indices‬
‭selected_indices = listbox.curselection()‬
‭# get selected items‬
s‭ elected_langs = ",".join([listbox.get(i) for i in selected_indices])‬
‭msg = 'You selected: ' + selected_langs‬
‭showinfo(title='Information', message=msg)‬

‭listbox.bind('<<ListboxSelect>>', items_selected)‬

‭root.mainloop()‬

‭21. yes/ no‬

i‭mport tkinter as tk‬


‭from tkinter import ttk‬
‭from tkinter.messagebox import askyesno‬

#‭ create the root window‬


‭root = tk.Tk()‬
‭root.title('Tkinter Yes/No Dialog')‬
‭root.geometry('300x150')‬

#‭ click event handler‬


‭def confirm():‬
‭answer = askyesno(title='confirmation',‬
‭message='Are you sure that you want to quit?')‬
‭if answer:‬
‭root.destroy()‬

‭ttk.Button(‬
‭root,‬
‭text='Ask Yes/No',‬
‭command=confirm).pack(expand=True)‬

#‭ start the app‬


‭root.mainloop()‬

‭22. ok cancel‬

i‭mport tkinter as tk‬


‭from tkinter import ttk‬
‭from tkinter.messagebox import askokcancel, showinfo, WARNING‬

#‭ create the root window‬


‭root = tk.Tk()‬
‭root.title('Tkinter Ok/Cancel Dialog')‬
‭root.geometry('300x150')‬
‭# click event handler‬

‭def confirm():‬
‭answer = askokcancel(‬
‭title='Confirmation',‬
‭message='Deleting will delete all the data.',‬
‭icon=WARNING)‬

‭if answer:‬
‭showinfo(‬
‭title='Deletion Status',‬
‭message='The data is deleted successfully')‬

‭ttk.Button(‬
‭root,‬
‭text='Delete All',‬
‭command=confirm).pack(expand=True)‬

#‭ start the app‬


‭root.mainloop()‬

‭23. Absolute positioning‬

‭from tkinter import *‬

‭def readvar():‬
‭print(x.get())‬

‭def changevar():‬
‭name=input("Enter a name :")‬
‭x.set(name)‬

t‭ op = Tk()‬
‭x = StringVar()‬
‭x.set("xyz")‬

e‭ ntry = Entry(top, textvariable=x)‬


‭entry.place(x=50,y=50)‬

‭ 1 = Button(top, text="Read" , command=readvar)‬


b
‭b1.place(x=50,y=100)‬
‭ 2 = Button(top, text="Change", command=changevar)‬
b
‭b2.place(x=125,y=100,width=50, height=50)‬
‭top.mainloop()‬

‭https://www.pythontutorial.net/tkinter/tkinter-hello-world/‬

You might also like