1 of 44

GUI Programming�Python

Satishkumar L. Varma

Professor, Department of Computer Engineering

PCE, New Panvel

www.sites.google.com/site/vsat2k

2 of 44

Outline

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

  • What is Python
  • IDE
  • GUI Library
  • Tkinter Programming
  • Tkinter Widgets
  • Add Widget GUI

2

3 of 44

What is Python

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

3

  • Python created by Guido van Rossum and first released in 1991
  • Employed by Google from 2005 until Dec 2012
  • He started working on Dropbox in 2013
  • Use English keywords rather than punctuation
  • Origin of name comes from Monty Python
  • Platform independent
  • Interpreted language
  • https://www.python.org/downloads/

4 of 44

IDE

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

  • IDLE
  • PythonWin
  • PyCharm
  • Wing
  • Spider

4

5 of 44

GUI Programming

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

  • Tk (Tkinter)
  • Qt (PyQt)
  • wxWidgets (wxPython)
  • Gtk (PyGtk)
  • Java Foundation Classes (JFC) (java.swing in Jython)
  • Microsoft Foundation Classes (PythonWin)

5

6 of 44

GUI Programming

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

  • Tkinter: It is Python interface to the Tk GUI toolkit shipped with Python.
  • wxPython: This is an open-source Python interface for wxWindows http://wxpython.org
  • JPython: It is a Python port for Java, which gives Python scripts seamless access to Java class libraries on the local machine http://www.jython.org
  • PyQt : Also a Python i/f for a popular cross-platform Qt GUI library
  • EasyGui: Here all GUI interactions are invoked by simple function calls
  • Kivy : Open source Python library for rapid development of applications that make use of innovative user interfaces, such as multi-touch apps

6

7 of 44

What is Tkinter

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

  • Tkinter module is Tk interface
  • It is standard Python interface to Tk GUI toolkit
  • It was developed by Sun Lab
  • It consists of number of modules
  • Before use import the Tkinter module

7

8 of 44

Tcl and Tk

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

  • Tcl
    • A general purpose multi-paradigm system progg. language
    • A scripting language to provide the ability for applications to communicate with each other
  • Tk
    • A cross platform widget toolkit used for building GUI in many languages

8

9 of 44

Tkinter Programming

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

  • Tkinter is the standard GUI library for Python.
  • IT provides a fast and easy way to create GUI app
  • It provides a powerful OO interface to the Tk GUI toolkit.
  • Perform the following steps −
    • Import the Tkinter module
    • Create the GUI application main window
    • Add one/more of the above-mentioned widgets to the GUI app
    • Enter the main event loop to take action against each event triggered by the user

9

10 of 44

Tkinter Widgets and Hierarchy

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

  • Tkinter provides various controls, such as buttons, labels in GUI
  • These controls are commonly called widgets

10

11 of 44

Types of Tkinter Widgets

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

  • Types of Operators in Tkinter

11

SN

Widget

Operator & Description

1

Button

The Button widget is used to display the buttons in your application.

2

Canvas

The Canvas widget is used to draw shapes, such as lines, ovals, polygons and rectangles, in your application.

3

Checkbutton

The Checkbutton widget is used to display a number of options as checkboxes. The user can select multiple options at a time.

4

Entry

The Entry widget is used to display a single-line text field for accepting values from a user.

5

Frame

Frame widget is used as a container widget to organize other widgets.

6

Label

The Label widget is used to provide a single-line caption for other widgets. It can also contain images.

7

Listbox

The Listbox widget is used to provide a list of options to a user.

8

Menubutton

The Menubutton widget is used to display menus in your application.

9

Menu

The Menu widget is used to provide various commands to a user. These commands are contained inside Menubutton.

10

Message

The Message widget is used to display multiline text fields for accepting values from a user.

12 of 44

Types of Tkinter Widgets

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

  • Types of widgets in Tkinter

12

SN

Widget (Operator)

Operator & Description

11

Radiobutton

The Radiobutton widget is used to display a number of options as radio buttons. The user can select only one option at a time.

12

Scale

The Scale widget is used to provide a slider widget.

13

Scrollbar

The Scrollbar widget is used to add scrolling capability to various widgets, such as list boxes.

14

Text

The Text widget is used to display text in multiple lines.

15

Toplevel

Toplevel widget is used to provide a separate window container.

16

Spinbox

Spinbox widget is a variant of the standard Tkinter Entry widget, which can be used to select from a fixed number of values.

17

PanedWindow

A PanedWindow is a container widget that may contain any number of panes, arranged horizontally or vertically.

18

LabelFrame

A labelframe is a simple container widget. Its primary purpose is to act as a spacer or container for complex window layouts.

19

tkMessageBox

This module is used to display message boxes in your applications.

13 of 44

Standard Attributes

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

  • Some of their common attributes of Widget (Operator)
    • Dimensions
    • Colors
    • Fonts
    • Anchors
    • Relief styles
    • Bitmaps
    • Cursors

13

14 of 44

Geometry Management

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

  • All widgets have access to specific geometry mgmt. methods
  • It helps in organizing widgets throughout the parent widget area.
  • Tkinter exposes the following geometry manager classes:
    • pack, grid, and place
  • pack() Method: It organizes widgets in blocks before placing them in the parent widget
  • grid() Method : It organizes widgets in a table-like structure in the parent widget
  • place() Method : It organizes widgets by placing them in a specific position in the parent widget

14

15 of 44

Points To Remember

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

  • Module name has changed Tkinter (Python2) to tkinter (Python3)
  • pip install tkinter // install GUI package on cmd
  • import tkinter // to check pkg install or not on python terminal
  • C:\Path\Vsat Python>python test.py // execute from cmd
  • tab spacing is very important (intend)
  • Comment in Python
    • # This is my first program
    • "This is my first program"
    • '''This is my multiline comment'''

15

16 of 44

Let Us Begin

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

  • Open IDEL Shell
  • Create new file with extension .py
  • Example: myWindow.py

# Tkinter in Python 2 to tkinter in Python 3

import tkinter

top = tkinter.Tk()

# Code to add widgets will go here...

top.mainloop()

16

17 of 44

Add Widget GUI: Label, Entry and Button

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

import tkinter

myGUI = tkinter.Tk()

myGUI.title(‘Add 3 Widgets - Label, Entry and Button’)

label1 = tkinter.Label(myGUI, text="This is Label Widget")

txtentry = tkinter.Entry(myGUI)

btn = tkinter.Button(myGUI, text ="I am Button Widget (Click me)")

#Adding 3 widgets on myGUI

label1.pack()

txtentry.pack()

btn.pack()

17

18 of 44

Add Even on Button Widget

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

import tkinter

def close():

myGUI.destroy()

myGUI = tkinter.Tk()

myGUI.title(‘Add 3 Widgets - Label, Entry and Button’)

label1 = tkinter.Label(myGUI, text="This is Label Widget")

txtentry = tkinter.Entry(myGUI)

btn = tkinter.Button(myGUI, text ="Click me to close appln", command = close)

label1.pack()

txtentry.pack()

btn.pack()

18

19 of 44

Resize and Add Color to GUI

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

import tkinter

def close():

myGUI.destroy()

myGUI = tkinter.Tk()

myGUI.geometry('450x250')

myGUI.configure(background='light blue')

myGUI.title('Add 3 Widgets - Label, Entry and Button')

label1 = tkinter.Label(myGUI, text="This is Label Widget")

txtentry = tkinter.Entry(myGUI)

btn = tkinter.Button(myGUI, text ='Close' , command = close, fg='white', bg='blue')

label1.pack()

txtentry.pack()

btn.pack()

19

20 of 44

Add Canvas Widget to GUI

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

  • Supported items on Canvas widget:
  • arc: Creates an arc item.

coord = 10, 50, 240, 210

arc = canvas.create_arc(coord, start=0, extent=150, fill="blue")

  • image: Creates an image item - an instance of BitmapImage or the PhotoImage classes.

filename = PhotoImage(file = "sunshine.gif")

image = canvas.create_image(50, 50, anchor=NE, image=filename)

  • line: Creates a line item.

line = canvas.create_line(x0, y0, x1, y1, ..., xn, yn, options)

  • oval : Creates a circle or an ellipse at the given coordinates. oval = canvas.create_oval(x0, y0, x1, y1, options)
  • polygon: Creates a polygon item that must have at least three vertices.

oval = canvas.create_polygon(x0, y0, x1, y1,...xn, yn, options)

20

21 of 44

Add Canvas Widget to GUI

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

  • import tkinter as tk
  • from tkinter import *
  • main = tk.Tk()
  • main.geometry('550x350')
  • arcCanvas = tk.Canvas(main, bg="blue", height=150, width=300)
  • coord = 10, 50, 150, 210
  • arc = arcCanvas.create_arc(coord, start=0, extent=150, fill="red")
  • imageCanvas = tk.Canvas(main, bg="light yellow", height=250, width=150)
  • filename = tk.PhotoImage(file = "satish.png")
  • image = imageCanvas.create_image(150, 20, anchor=NE, image=filename)
  • arcCanvas.pack()
  • imageCanvas.pack()
  • main.mainloop()

21

22 of 44

Add more Widgets on GUI

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

import sys

from tkinter import *

myGUI = Tk()

nameentry = StringVar()

myGUI.geometry('450x250') # WxH

myGUI.title('Playing with the layout of Buttons')

Label1 = Label(myGUI, text='Enter Text Here').pack()

Textentry1 = Entry(myGUI, textvariable=nameentry).pack()

Button1 = Button(myGUI, text='Button 1').pack(side=LEFT,padx=20)

Button2 = Button(myGUI, text='Button 2').pack(side=LEFT,padx=5)

Button3 = Button(myGUI, text='Button 3').pack(side=RIGHT,padx=20)

Button4 = Button(myGUI, text='Button 4').pack(side=RIGHT,padx=5)

22

23 of 44

Widgets on GUI

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

from tkinter import *

myGUI = Tk()

nameentry = StringVar()

myGUI.geometry('450x250')# WxH

myGUI.configure(background='light blue')

myGUI.title('Login Screen')

Label1 = Label(myGUI, text='Username').pack()

textentry1= Entry(myGUI, textvariable=nameentry).pack()

Label2 = Label(myGUI, text='Password').pack()

textentry2 = Entry(myGUI).pack()

button1 = Button(myGUI, text='Login', fg = 'white', bg = 'blue').pack()

23

24 of 44

Align Widgets on GUI Using Pack()

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

from tkinter import *

myGUI = Tk()

nameentry = StringVar()

myGUI.geometry('450x250+400+100')# WxH

myGUI.configure(background='light blue')

myGUI.title('Login Screen')

Label1 = Label(myGUI, text='Username').pack()

textentry1= Entry(myGUI, textvariable=nameentry).pack()

Label2 = Label(myGUI, text='Password').pack(side=LEFT,padx=5)

textentry2 = Entry(myGUI).pack(side=LEFT,padx=50)

button1 = Button(myGUI, text='Login', fg = 'white', bg = 'blue').pack(side=RIGHT,padx=5)

24

25 of 44

Align Widgets on GUI using Place()

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

from tkinter import *

myGUI = Tk()

myGUI.configure(background='light blue')

myGUI.geometry('450x250+500+200')

myGUI.title('Login Screen')

Label1 = Label(myGUI, text='Username').place(x=20, y=20)

textentry1= Entry(myGUI).place(x=20, y=40)

Label2 = Label(myGUI, text='Password').place(x=20, y=60)

textentry2= Entry(myGUI).place(x=20, y=80)

button1=Button(myGUI, text='Login', fg = 'white', bg = 'blue').place(x=20,y=100)

25

26 of 44

Align Widgets on GUI using Grid()

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

from tkinter import *

myGUI = Tk()

myGUI.configure(background='light blue')

myGUI.geometry('450x250+500+200')

myGUI.title('Login Screen')

Label1 = Label(myGUI, text='Username').grid(row=0,column=0, sticky=N)

textentry1= Entry(myGUI).grid(row=0, column=1)

Label2 = Label(myGUI, text='Password').grid(row=1,column=0)

textentry2= Entry(myGUI).grid(row=1,column=1)

button1=Button(myGUI, text='Login', fg = 'white', bg = 'blue').grid(row=2,column=1)

26

27 of 44

Align Widgets on GUI using Pack() and Frame()

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

from tkinter import *

myGUI = Tk()

myGUI.geometry('450x250+500+200')

myGUI.title('Using a Frame')

label1 = Label(myGUI, text="Example of using frames:")

label1.pack()

f = Frame(myGUI)

button1 = Button(f, text="Button 1")

button2 = Button(f, text="Button 2")

button3 = Button(f, text="Button 3")

button1.pack(side=LEFT, padx=5)

button2.pack(side=LEFT)

button3.pack(side=RIGHT,pady=15,padx=50)

f.pack()

27

28 of 44

Align Widgets on GUI using Pack() and 2 Frames

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

from tkinter import *

myGUI = Tk()

myGUI.geometry('450x250+500+200')

myGUI.title('Using a Frame')

label1 = Label(myGUI, text="Example of using frames:")

label1.pack()

f = Frame(myGUI)

button1 = Button(f, text="Button 1")

button2 = Button(f, text="Button 2")

button1.pack(side=LEFT, padx=5)

button2.pack(side=RIGHT,pady=15,padx=50)

f.pack()

f1 = Frame(myGUI)

button1 = Button(f1, text="Close Button",fg='blue')

button1.pack(side=RIGHT,pady=50,padx=5)

f1.pack()

28

29 of 44

Two GUIs

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

from tkinter import *

def close():

myGUI.destroy()

myGUI = Tk()

myGUI.configure(background='light blue')

myGUI.geometry('450x200+500+200')

myGUI.title('Login Screen')

Label1 = Label(myGUI, text='Username').place(x=20, y=20)

textentry1= Entry(myGUI).place(x=20, y=40)

Label2 = Label(myGUI, text='Password').place(x=20, y=60)

textentry2= Entry(myGUI).place(x=20, y=80)

button1=Button(myGUI, text='Login', fg = 'white', bg = 'blue').place(x=20,y=100)

button2=Button(myGUI, text='Close', command=close,fg = 'white', bg = 'blue').place(x=300,y=150)

myGUI.mainloop()

###################################

window = Tk()

window.configure(background='light yellow')

window.geometry('450x150+450+450')

window.title('MY Second GUI')

29

30 of 44

Login Message Application (Handle Main and Login GUI)

  • import tkinter as tk
  • from tkinter import *
  • from tkinter import messagebox
  • def msg():
  • if nameEntry.get() == "user" and passEntry.get() == "pass":
  • messagebox.showinfo("Login Message","Welcome - Access Granted!")
  • main.deiconify()
  • login.destroy()
  • else:
  • messagebox.showwarning("Login Message","Sorry, Access Denied!")
  • def cancel():
  • login.destroy()
  • main.destroy()
  • sys.exit()
  • main = Tk()
  • login = Toplevel()
  • login.configure(background='light blue')
  • login.geometry('350x150+400+200')
  • login.title('Login Screen')
  • nameLabel = Label(login, text="Username")
  • nameEntry = Entry(login)
  • passLabel = Label(login, text="Password")
  • passEntry = Entry(login)
  • loginButton = Button(login, text='Login', command = lambda:msg())
  • cancelButton = Button(login, text='Cancel', command = lambda:cancel())
  • main.geometry('350x400+400+200')
  • mainLabel = Label(main, text="Welcome to Application").pack()
  • imageCanvas = tk.Canvas(main, bg="light yellow", height=250, width=150)
  • filename = tk.PhotoImage(file = "satish.png")
  • image = imageCanvas.create_image(150, 10, anchor=NE, image=filename)
  • cancelMainButton = Button(main, text='Close', command = lambda:cancel())
  • nameLabel.pack()
  • nameEntry.pack()
  • passLabel.pack()
  • passEntry.pack()
  • cancelButton.pack(side=RIGHT, padx=15, pady=20)
  • loginButton.pack(side=RIGHT, padx=15, pady=2)
  • imageCanvas.pack()
  • cancelMainButton.pack()
  • #To hides the main window (still present but can't be seen/interacted)
  • main.withdraw()
  • #Starts the event loop for the main window
  • main.mainloop()

30

31 of 44

Tkinter Checkbutton

  • from tkinter import *
  • import tkinter
  • from tkinter import messagebox
  • def answer():
  • if str(CheckVar1.get()) == '1' and CheckVar2.get() == 1 and CheckVar3.get() == 0 and CheckVar4.get() == 0:
  • result.set("Correct answer! - Very Good!")
  • messagebox.showinfo("Success Message","Correct answer! - Very Good!")
  • else:
  • result.set("Wrong answer! Try again.")
  • messagebox.showwarning("Failure Message","Wrong answer! Try again.")
  • main = Tk()
  • main.geometry('400x300+400+200')
  • main.title('Checkbutton Example')
  • CheckVar1 = IntVar()
  • CheckVar2 = IntVar()
  • CheckVar3 = IntVar()
  • CheckVar4 = IntVar()
  • queLabel = Label(main, text='Which is/are Verb words:')
  • C1 = Checkbutton(main, text = "Go", variable = CheckVar1, \
  • onvalue = 1, offvalue = 0, height=2, width = 5,relief=RAISED)
  • C2 = Checkbutton(main, text = "Put", variable = CheckVar2, \
  • onvalue = 1, offvalue = 0, height=2, width = 5,relief=RAISED)
  • C3 = Checkbutton(main, text = "Zoo", variable = CheckVar3, \
  • onvalue = 1, offvalue = 0, height=2, width = 5,relief=RAISED)
  • C4 = Checkbutton(main, text = "Tin", variable = CheckVar4, \
  • onvalue = 1, offvalue = 0, height=2, width = 5,relief=RAISED)
  • print("Value of CheckVar1 = ",str(CheckVar1.get()))
  • loginButton = Button(main, text='Submit Answer', command = lambda:answer())
  • queLabel.pack()
  • C1.pack()
  • C2.pack()
  • C3.pack()
  • C4.pack()
  • result = StringVar()
  • resultLabel = Label(main, textvariable=result,relief=SUNKEN).pack(side=LEFT, padx=15, pady=2)
  • loginButton.pack(side=RIGHT, padx=15, pady=2)
  • main.mainloop()

31

32 of 44

Tkinter Listbox

from tkinter import *

import tkinter

def selectItem():

if 1 == 1:

result.set("Selected Item is : ")

else:

result.set("Not selected!")

main = Tk()

main.geometry('450x350+400+200')

imageCanvas = Canvas(main, bg="light yellow", height=250, width=150)

filename = PhotoImage(file = "satish.png")

image = imageCanvas.create_image(150, 20, anchor=NE, image=filename)

Lb1 = Listbox(main)

Lb1.insert(1, "Satish")

Lb1.insert(2, "Harsh")

Lb1.insert(3, "Saransh")

Lb1.insert(4, "Saransh + Harsh")

result = StringVar()

resultLabel = Label(main, textvariable=result,relief=SUNKEN)

#print("Value of ListVar1 = ",str(Lb1.get()))

resultLabel.grid(row=1, column=1,pady=20)

Lb1.grid(row=0, column=0, padx=50)

imageCanvas.grid(row=0, column=1,pady=20)

main.mainloop()

32

33 of 44

Tkinter Menubutton

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

  • from tkinter import *
  • import tkinter
  • main = Tk()
  • main.geometry('400x200')
  • mb = Menubutton(main, text="File", relief=RAISED)
  • mb.grid()
  • mb.menu = Menu(mb, tearoff = 0)
  • mb["menu"] = mb.menu
  • fileOpen = IntVar()
  • close = IntVar()
  • mb.menu.add_checkbutton(label='FileOpen', variable=fileOpen)
  • mb.menu.add_checkbutton(label='Close', variable=close)
  • mb.pack(side=TOP,pady=2)
  • main.mainloop()

33

34 of 44

Tkinter Message

  • It provides a multiline and noneditable object that displays texts
  • Automatically breaking lines and justifying their contents
  • Similar to the one provided by the Label widget
  • Except that it automatically wrap the text
  • Maintain a given width or aspect ratio

from tkinter import *

main = Tk()

main.geometry('400x300')

var = StringVar()

label = Message(main, textvariable=var, relief=RAISED )

var.set("Like def, the lambda creates a function \

to be called later. But it returns the function \

instead of assigning it to a name. This is why \

lambdas are sometimes known as anonymous functions. \

In practice, they are used as a way to inline a \

function definition, or to defer execution of a code.")

label.pack(side=LEFT, padx=10, pady=5)

main.mainloop()

34

35 of 44

Tkinter Scale

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

  • Scale widget provides a graphical slider object
  • It allows you to select values from a specific scale

from tkinter import *

def sel():

selection = "Value = " + str(var.get())

label.config(text = selection)

main = Tk()

main.geometry('400x200+300+200')

main.title('Scale Widget')

var = DoubleVar()

scale = Scale(main, variable = var )

scale.pack(anchor=CENTER,pady=10)

button = Button(main, text="Get Scale Value", command=sel)

button.pack(anchor=CENTER)

label = Label(main,fg='blue')

label.pack()

main.mainloop()

35

36 of 44

Tkinter Scrollbar

  • It provides a slide controller
  • It is used to implement vertical scrolled widgets such as
    • Listbox, Text, and Canvas
  • We can also create horizontal scrollbars on Entry widgets

from tkinter import *

main = Tk()

main.geometry('500x300+400+200')

main.title('Scrollbar Widget')

scrollbar = Scrollbar(main)

scrollbar.pack(side = LEFT, fill=Y)

mylist = Listbox(main, yscrollcommand = scrollbar.set )

for line in range(50):

mylist.insert(END, "Line number is " + str(line))

mylist.pack(side = LEFT, fill = BOTH,padx=20,pady=5)

scrollbar.config(command = mylist.yview )

imageCanvas = Canvas(main, bg="light yellow", height=250, width=150)

filename = PhotoImage(file = "satish.png")

image = imageCanvas.create_image(150, 20, anchor=NE, image=filename)

imageCanvas.pack(padx=20,pady=5)

36

37 of 44

Tkinter Text

  • Text widgets provide advanced capabilities
  • It allow to edit a multiline text and
  • Format the way it has to be displayed (color, font, etc.)

from tkinter import *

def onclick():

pass

main = Tk()

main.geometry('500x300+400+200')

main.title('Text Widget')

text = Text(main,pady=5)

text.insert(INSERT, "Hello.....")

text.insert(END, "Bye Bye.....")

text.pack()

text.tag_add("here", "1.0", "1.4")

text.tag_add("start", "1.6", "1.13")

text.tag_config("here", background="yellow", foreground="blue")

text.tag_config("start", background="blue", foreground="green")

main.mainloop()

37

38 of 44

Tkinter Spinbox

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

from tkinter import *

main = Tk()

main.geometry('400x200+400+200')

main.title('Spinbox Widget')

w = Spinbox(main, from_=0, to=10)

w.pack(pady=5)

mainloop()

38

39 of 44

Tkinter PanedWindow

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

  • It is a container widget
  • It contain any number of panes, arranged horizontally/vertically
  • Each pane contains one widget, and
  • Each pair of panes is separated by a moveable (via mouse movements) sash
  • Moving a sash causes the widgets on either side of the sash to be resized

from tkinter import *

main = Tk()

main.geometry('400x200+400+200')

main.title('PanedWindow Widget')

m1 = PanedWindow()

m1.pack(fill=BOTH, expand=1)

left = Label(m1, text="left pane 1",bg='red')

m1.add(left)

m2 = PanedWindow(m1, orient=VERTICAL)

m1.add(m2)

top = Label(m2, text="top pane 1/2",bg='blue')

m2.add(top)

bottom = Label(m2, text="bottom pane 2/2",bg='yellow')

m2.add(bottom)

main.mainloop()

39

40 of 44

Tkinter LabelFrame

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

  • It is a simple container widget
  • Purpose is to act as a spacer/container for complex window layouts
  • This widget has the features of a frame plus the ability to display a label

from tkinter import *

main = Tk()

main.geometry('400x200+400+200')

main.title('PanedWindow Widget')

labelframe = LabelFrame(main, text="This is a LabelFrame")

labelframe.pack(fill="both", expand="yes")

left = Label(labelframe, text="Inside the LabelFrame")

left.pack()

main.mainloop()

40

41 of 44

PanedWindow and LabelFrame Widget

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

  • from tkinter import *
  • main = Tk()
  • main.geometry('400x200+400+200')
  • main.title('PanedWindow and LabelFrame Widget')
  • m1 = PanedWindow()
  • m1.pack(fill=BOTH, expand=1)
  • left = Label(m1, text="left pane 1",bg='red')
  • m1.add(left)
  • m2 = PanedWindow(m1, orient=VERTICAL)
  • m1.add(m2)
  • top = Label(m2, text="top pane 1/2",bg='blue')
  • m2.add(top)
  • bottom = LabelFrame(main, text="This is a LabelFrame")
  • bottom.pack(fill="both", expand="yes")
  • left = Label(bottom, text="Inside the LabelFrame", fg='white', bg='green')
  • left.pack()
  • m2.add(bottom)
  • main.mainloop()

41

42 of 44

Outline

Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k

42

43 of 44

References

  • Zed A. Shaw, “Learn Python the Hard Way”, Third Edition, Addison-Wesley, 2014.
  • Bruno Dufour, “A Comprehensive Introduction to Python Programming and GUI Design Using Tkinter”, McGill Univeristy SOCS.
  • JOHN E. GRAYSON, “Python and Tkinter Programming”, Manning Publications, 2000.
  • Burkhard A. Meier, “Python GUI Programming Cookbook”, Packt Publishing, 2015.

43

Satishkumar Varma, PCE www.sites.google.com/site/vsat2k

44 of 44

Thank You.

Satishkumar Varma, PCE www.sites.google.com/site/vsat2k

44