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
Give a title
from tkinter import *
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()
#Create Label, Button and Textbox with Entry
# use of .place( ) to fix them ***
from tkinter import *
win = Tk()
win.geometry("400x250")
#creating a label
username = Label(win, text = "Username").place(x = 30,y = 50)
#creating second label
password = Label(win, text = "Password").place(x = 30, y = 90)
submitbutton = Button(win, text = "Submit",fg = "red", bg = "blue").place(x =
30, y = 120)
win = Tk()
win.geometry("500x500+10+20")
mainloop()
Button and apply tkMessageBox
on its command
The anchor
Canvas
• The Canvas is a rectangular area intended for
drawing pictures or other complex layouts.
We can place graphics, text, widgets or
frames on a Canvas.
• program3 import tkinter
top =tkinter.Tk()
• mainloop()
For Student’s Practice
Further widgets are:
• Listbox
• Radiobutton
• Checkbutton
• Menubutton
• Spinbox
• Scale
• Message
• photoImage
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, list_variable)
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
• x = StringVar() # Holds a string; default value ""
• x = IntVar() # Holds an integer; default value 0
• x = DoubleVar() # Holds a float; default value 0.0
• x = BooleanVar() # Holds a boolean, returns 0 for False
and 1 for True
label = Label(root)
label.pack()
root.mainloop()
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()
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()
x1 = IntVar()
y1 = IntVar()
mb.place(x=100, y=100)
mainloop()
Use add_command, add_cascade
to config a menu
Checkbutton
• The Checkbutton widget is used to display a
number of options to a user as toggle buttons.
The user can then select one or more options
by clicking the button corresponding to each
option.
top = Tk()
CheckVar1 = IntVar()
CheckVar2 = IntVar()
C1.pack()
C2.pack()
mainloop()
• sp.pack()
• mainloop()
wrap
• The wrap is a Boolean value.
• If wrap equals True , when the current value
reaches the maximum value, it's set to the
lowest value if you click the upward-pointing
arrowhead and vice versa.
• In case wrap equals False , it's set to the
maximum value if you click the downward-
pointing arrowhead.
Scale
• The Scale widget provides a graphical slider
object that allows you to select values from a
specific scale.
• Syntax
w = Scale ( master, option, ... )
Message
• This widget provides a multiline and non-
editable object that displays texts,
automatically breaking lines and justifying
their contents.
• Its functionality is very similar to the one
provided by the Label widget, except that it
can also automatically wrap the text,
maintaining a given width or aspect ratio.
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.
import PIL
• PIL is the Python Imaging Library which
provides the python interpreter with image
editing capabilities.
• # Use images as Label
• from tkinter import *
• from PIL import Image, ImageTk # PIL is Python Imaging Library
• root = Tk()
•
• mg = Image.open("abc.gif") # import Image
•
• # Resize the image using resize() method
• mg1 = mg.resize((30, 30)) #use resize
•
• im = ImageTk.PhotoImage(mg1)
• x = Label(image = im)
• x.image = im
• x.pack()
• mainloop()
Bring Image
from tkinter import *
top = Tk()
Program
#Import the image using PhotoImage function
im= PhotoImage(file='abc.gif')
mainloop()
Images at Buttons
• A bitmap is an array of binary data
representing the values of pixels in an image.
A GIF is an example of a graphics image file
that has a bitmap.
• To create a bitmap image ‘bitmap’ attribute of
the button() function is used display. It can
take the following values:
• # Use images as Label, 2 different images at two labels
• from tkinter import *
• from PIL import Image, ImageTk # PIL is Python Imaging Library
• root = Tk()
• root.geometry("500x500")
• mg = Image.open('abc.gif') # import Image
• mg1 = Image.open('D:\py.gif')
• mg = mg.resize((70, 70)) #use resize
• mg1 = mg1.resize((70, 70))
• im = ImageTk.PhotoImage(mg)
• im1 = ImageTk.PhotoImage(mg1)
• x = Label(image = im)
• y = Label(image = im1)
• x.image = im
• y.image = im1
• x.pack(side=LEFT)
• y.pack(side=RIGHT)
• mainloop()
Easy Code of Popular bitmaps at
buttons
• #images at Buttons (use bitmap)
• from tkinter import *
• root = Tk()
• Button(bitmap="question").pack(pady=30)
• Button(bitmap="questhead").pack(pady=10)
• Button(bitmap="warning"). pack(pady=30)
• Button(bitmap="error") . pack(pady=10)
• Button(bitmap="info").pack(pady=10)
• mainloop()
• #images at Buttons (use bitmap)
• from tkinter import *
• root = Tk()
• Button(root, relief=GROOVE, bitmap="question", bd=10).pack(pady=30)