Tkinter - Students 4 UNIT CH2 GUI Using Python
Tkinter - Students 4 UNIT CH2 GUI Using Python
Tkinter:
is a Standard Python Library/GUI
• It is default GUI of python
• It comes with many functions and
methods that can be used to create
an application.
• #Import tkinter library
• from tkinter import *
• Import tkinter
Tkinter Programming
• Tkinter is the standard GUI library for Python.
• Python when combined with Tkinter provides
a fast and easy way to create GUI applications.
• Tkinter provides a powerful object-oriented
interface to the Tk GUI toolkit.
• Creating a GUI application using Tkinter is an
easy task.
•All you need to do is perform the following steps −
–Import the Tkinter module.
–Create the GUI application main window.
–Add one or more of the widgets to the GUI application.
–Enter the main event loop to take action against each event triggered by the
user.
Various GUI in Python
• Tkinter
• PyGUI
• PySimpleGUI
• Kivy
• PyForms
• Wax
Tk( )
• It manages all the other components of the
tkinter application.
• Use it to create instance of tkinter
• It helps to display the root window
• Tk is a interface inside the tkinter package (“Tk
interface”) is the standard Python interface to
the Tcl/Tk GUI toolkit.
Tkinter is Tk interface
• The Tk application object created by instantiating Tk.
• This provides access to the Tcl (Tool Command
Language) interpreter.
mainloop()
• As the name implies it will loop forever until the user
exits the window or waits for any events from the
user.
• mainloop() is simply a method in the main window
that executes what we wish to execute in an
application
Must use Tk() GUI toolkit
• Program1
Width=700
height=500
x=60
y=40
root.geometry(‘ 600x400 + 60 + 40 ')
Make fixed width height
• Use it as:
• window.resizable(width,height)
• root.geometry('600x400')
• root.resizable(False, False)
Tkinter Widgets
• Tkinter provides various controls, such as
buttons, labels and text boxes used in a GUI
application.
• These controls are commonly called widgets.
• There are currently 15 types of widgets in
Tkinter.
Standard attributes
• Let us take a look at how some of their common
attributes.
• Such as sizes, colors and fonts are specified.
– dimensions
– colors
– fonts
– anchors
– relief styles
– bitmaps
– cursors
Give a title
from tkinter import *
win.mainloop()
# add 3 frames and pack( ) each frame ***
import tkinter as tk
win = tk.Tk()
win.geometry("500x500+10+20")
# add an orange frame
frame1 = tk.Frame(master=win, width=100, height=100, bg="pink")
frame1.pack()
window.mainloop()
Button and apply tkMessageBox
• Program2
on its command
import Tkinter
import tkMessageBox
top = Tkinter.Tk()
def helloCallBack():
tkMessageBox.showinfo( "Hello Python", "Hello World")
top =tkinter.Tk()
top.mainloop()
Frame
from Tkinter import *
root = Tk()
• program5 frame = Frame(root)
frame.pack()
bottomframe = Frame(root)
bottomframe.pack( side = BOTTOM )
redbutton = Button(frame, text="Red", fg="red")
redbutton.pack( side = LEFT)
root.mainloop()
The Listbox ( )
• It displays a list of text items.
• A Listbox allows to select one or multiple items at a time.
• Use the Listbox(container, height, listvariable)
to create a Listbox widget;
• a listvariable should be a StringVar(value=items).
Lb1 = Listbox(top)
Lb1.insert(1, "Python")
Lb1.insert(2, "Perl")
Lb1.insert(3, "C")
Lb1.insert(4, "PHP")
Lb1.insert(5, "JSP")
Lb1.insert(6, "Ruby")
Lb1.pack()
top.mainloop()
RadioButton
• Many buttons option, we can click on one
• Use tkinter variables (mainly
– IntVar and
– StringVar) to access its state.
• Also called option button
Code
• # Use Radiobutton in tkinter
• from tkinter import *
• root = Tk()
• x = IntVar()
• R1 = Radiobutton(root, text="Option 1", variable = x, value =1)
• R1.pack()
• mainloop()
Tkinter IntVar() Function
label = Label(root)
label.pack()
root.mainloop()
Menubutton
• The Menubutton widget can be defined as
the drop-down menu that is shown to the
user all the time.
Menubutton
• Place Menu ( ) in Tk
• Now config()
• Use add_command()
• t1=Menubutton(parent_window,options)
• program8
Menubutton
from tkinter import *
import tkMessageBox
import tkinter
top = Tk()
mb= Menubutton ( top, text="condiments“,relief=RAISED )
mb.grid()
mb.menu = Menu ( mb, tearoff = 0 )
mb["menu"] = mb.menu
mayoVar = IntVar()
ketchVar = IntVar()
mb.menu.add_checkbutton ( label="mayo",
variable=mayoVar )
mb.menu.add_checkbutton ( label="ketchup",
variable=ketchVar )
mb.pack()
top.mainloop()
Check button
from Tkinter import *
import tkMessageBox
program9 import Tkinter
top = Tkinter.Tk()
CheckVar1 = IntVar()
CheckVar2 = IntVar()
C1 = Checkbutton(top, text = "Music", variable = CheckVar1,
onvalue = 1, offvalue = 0, height=5, width = 20)
C2 = Checkbutton(top, text = "Video", variable = CheckVar2,
onvalue = 1, offvalue = 0, height=15, width = 50)
C1.pack()
C2.pack()
top.mainloop()
Image
• To display images in labels, buttons, canvas,
and text widgets, the PhotoImage class is
used, which is present in tkinter package.
• "PhotoImage()" function returns the image
object.
Bring Image
# Putting a gif image on a canvas with tkinter