Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

PYTHON ADVANCED – WORKSHEET

(Session – 4)

Tkinter is Python’s default GUI library. Python, along with Tkinter,


provides a fast and exciting way to build useful applications. We will
access Tk from its Python interface called Tkinter (short for “Tk
interface”).

There are basically five main steps that are required to get your GUI up
and running:
1. Import the Tkinter module (or from Tkinter import *).
2. Create a top-level windowing object that contains your entire GUI
application.
3. Build all your GUI components (and functionality) on top (or
within) of your top-level windowing object.
4. Connect these GUI components to the underlying application
code.
5. Enter the main event loop.

In GUI programming, a top-level root windowing object contains all of


the little windowing objects that will be part of your complete GUI
application. These can be text labels, buttons, list boxes, etc. These
individual little GUI components are known as widgets.
Adding a Label -
from tkinter import *
root = Tk()
w = Label(root, text = "Hello")
w.pack()
root.mainloop()
Event sources (widgets) can specify their handlers:
• command handlers
• callbacks
• binds
Command Handler –
from Tkinter import *
root = Tk()
Button (root, text='Press Me', command=root.quit).pack(side=LEFT)
root.mainloop()

Callbacks -
from Tkinter import *
root = Tk()
def quit():
print 'Hello, getting out of here’
import sys; sys.exit()

widget = Button(root, text='Press me to quit' , command=quit)


widget.pack()
root.mainloop()

Binds –
import tkinter as tk
root = Tk()
def hello(event):
print('Double click to exit')

def quit(event):
print ('caught a double click, leaving')
import sys
sys.exit()

widget = tk.Button(root, text='Hello Event World')


widget.pack()
widget.bind('<1>',hello)
widget.bind('<Double-1>',quit)
root.mainloop()

Adding Image to Label


logo = tk.PhotoImage(file = "parts_of_computer.gif")
w1 = tk.Label(root, image=logo)

MessageBox
• showinfo()
The showinfo() messagebox is used where we need to show some
relevant information to the user.
• showwarning()
This method is used to display the warning to the user.
• showerror()
This method is used to display the error message to the user.
Consider the following example.
• askquestion()
This method is used to ask some question to the user which can
be answered in yes or no.
• askokcancel()
This method is used to confirm the user's action regarding some
application activity.
• askyesno()
This method is used to ask the user about some action to which,
the user can answer in yes or no.
• askretrycancel()
This method is used to ask the user about doing a particular task
again or not.

from tkinter import *


from tkinter import messagebox

root = Tk()
messagebox.showinfo("information","Information")
root.pack()
root.mainloop()

ASSIGNMENTS

ASSIGNMENT 1 –
Justifying Text on Label and Adding Padding around Text

mytext = """
There are different parts of the computer.
They are : CPU, Monitor, Keyboard and Mouse.
"""

w2 = tk.Label(root, text=mytext, justify = tk.CENTER, padx = 50)


w2.pack(side="left")
ASSIGNMENT 2 -

More on Buttons
from tkinter import *
from tkinter import messagebox

def displayred():
messagebox.showinfo("Hello", "Red Button clicked")

root = Tk()
root.title("Colourful Buttons")
rbutton = Button(root, text='RED', activeforeground =
"red",activebackground = "pink", width=25, command=displayred)
rbutton.pack(side = "left")

root.mainloop()

Refer to the code above and write a GUI program to create 4


buttons – RED, BLUE, GREEN, YELLOW
- pack the buttons to left, right, top and bottom
- set the activeforeground and activebackground for different
colours
- call different command callbacks for each of the buttons

You might also like