You are on page 1of 11

GUI Programming

• GUI stands for graphical user interface, a GUI is an interface that uses icons or other
visual indicators to interact with electronic devices, rather than only text via a command
line.
• For example, all versions of Microsoft Windows is a GUI, whereas MS-DOS is a
command line.

Tkinter

1. Tk was developed as a GUI extension for the Tcl scripting language by John Ousterhout.
2. The first release was in 1991. Tk proved as extremely successful in the 1990's, because it
is easier to learn and to use than other toolkits.
3. Tkinter is an inbuilt Python module used to create simple GUI apps.
4. It is the most commonly used module for GUI apps in the Python.
5. The name Tkinter comes from Tk interface.

Opening New Window

First GUI Program


import tkinter

window = tkinter.Tk()
window.title("GUI") # to rename the title of the window
# pack is used to show the object in the window
label = tkinter.Label(window, text = "Hello World!").pack( )
window.mainloop( )

• import the module tkinter.


• Initialize the window manager with the tkinter.Tk( ) method and assign it to a
variable window. This method creates a blank window with close, maximize and
minimize buttons.
• Rename the title of the window as you like with the window.title(title_of_the_window).
• Label is used to insert some objects into the window. Here, we are adding a Label with
some text.
• pack() attribute of the widget is used to display the widget in a size it requires.
• Finally, the mainloop( ) method to display the window until you manually close it.

Placing Widgets or Component in Window

1. Tkinter allows to access the geometric configuration of the widgets which can organize
the widgets in the parent windows.
2. There are mainly three geometry manager classes class.
• pack( ) method: It organizes the widgets in blocks before placing in the parent
widget.
• grid( ) method: It organizes the widgets in grid (table-like structure) before
placing in the parent widget.
• place( ) method: It organizes the widgets by placing them on specific positions
directed by the programmer.

Tkinter Widgets

1. Button - The Button widget is used to display 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 - The 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.
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 -The Toplevel widget is used to provide a separate window container.
16. Spinbox -The 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.

Standard attributes
Common attributes of the widgets such as sizes, colors and fonts are specified.

• Dimensions
• Colors
• Fonts
• Anchors
• Relief styles
• Bitmaps
• Cursors

Label

It refers to the display box where you can put any text or image which can be updated any time
as per the code.
Syntax :

w=Label(master, option=value)

master is the parameter used to represent the parent window.

• bg: to set the normal background color.


• command: to call a function.
• font: to set the font on the button label.
• image: to set the image on the button.
• width: to set the width of the button.
• height” to set the height of the button.

Example

from tkinter import *


root = Tk()
w = Label(root, text='This is Label')
w.pack( )
root.mainloop( )
Button

To add a button in your application, this widget is used.

The general syntax is:

w=Button(master, option=value)

• activebackground: to set the background color when button is under the cursor.
• activeforeground: to set the foreground color when button is under the cursor.
• bg: to set he normal background color.
• command: to call a function.
• font: to set the font on the button label.
• image: to set the image on the button.
• width: to set the width of the button.
• height: to set the height of the button.

Example-2 for Button

import tkinter as tk
r = tk.Tk()
r.title('Counting Seconds')
button = tk.Button(r, text='Stop', width=25, command=r.destroy)
button.pack()
r.mainloop()
Example-2 for Button

import tkinter as tk
def show():
tk.Message(r,text="Hello", fg='red', font='Arial', bg='yellow', relief=tk.RAISED).pack()
return
r = tk.Tk()
r.title('Show Message')
button = tk.Button(r, text='Click Her
Here',
e', font='Arial', fg='brown', width=25, command=show)
button.pack()
r.mainloop()

Entry

It is used to input the single line text entry from the user.. For multi
multi-line
line text input, Text widget
is used.

Syntax

w=Entry(master, option=value)
• bd: to set the border width in pixels.
• bg: to set the normal background color.
• cursor: to set the cursor used.
• command: to call a function.
• highlightcolor: to set the color shown in the focus highlight.
• width: to set the width of the button.
• height: to set the height of the button.

Example

from tkinter import *


master = Tk()
Label(master, text='First Name').grid(row=0)
Label(master, text='Last Name').grid(row=1)
e1 = Entry(master)
e2 = Entry(master)
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
mainloop()

Exampl-2 for Entry

from tkinter import *


def show():
age=(vAge.get())
if age>=18: msg= "Hello "+ vName.get() + ", you are eligible to Vote !"
else: msg= "Hello "+ vName.get() + ", you are not eligible to Vote !"
m1=Message(master,text=msg, fg='red', bg='yellow', width=100,relief=RAISED)
m1.grid(row=3 , columnspan=2)
return
master = Tk()
Label(master, text='First Name').grid(row=0)
Label(master, text='Age').grid(row=1)
vName=StringVar()
vAge=IntVar()
e1=Entry(master, textvariable=vName).grid(row=0, column=1)
e2=Entry(master, textvariable=vAge).grid(row=1, column=1)
b1 = Button(master, text='Click Here', font='Arial‘, fg='brown', width=25, command=show)
b1.grid(row=2,columnspan=2)
mainloop()

CheckButton

To select any number of options by displaying a number of options to a user as toggle buttons.

Syntax :

w = CheckButton(master, option=value)

• Title: To set the title of the widget.


• activebackground: to set the background color when widget is under the cursor.
• activeforeground: to set the foreground color when widget is under the cursor.
• bg: to set he normal backgrouSteganography
• command: to call a function.
• font: to set the font on the button label.
• image: to set the image on the widget.

Example-1 for Check Button


from tkinter import *
master = Tk()
var1 = IntVar()
Checkbutton(master, text='male', variable=var1).grid(row=0, sticky=W)
var2 = IntVar()
Checkbutton(master, text='female', variable=var2).grid(row=1, sticky=W)
mainloop()
Example-2 for Check Button

from tkinter import *


def show():
v1=int(var1.get())
v2=int(var2.get())
if v1==1 and v2==1 : msg.set( "You selected both")
if v1==1 and v2==0: msg.set( "You selected Chocolate")
if v1==0 and v2==1: msg.set( "You selected IceCream")
if v1==0 and v2==0: msg.set( "You selected None")
return
master = Tk()
var1 = IntVar()
var2 = IntVar()
msg=StringVar()
Checkbutton(master, text='Chocolate', variable=var1).grid(row=1, sticky=W)
Checkbutton(master, text='IceCream', variable=var2).grid(row=2, sticky=W)
b1 = Button(master, text='Click Here', font='Arial‘, fg='blue', width=25, command=show)
b1.grid(row=3,columnspan=2)
Label(master, fg='red', font='Arial', textvariable=msg).grid(row=4 , column=1)
mainloop()

RadioButton

It is used to offer multi-choice option to the user. It offers several options to the user and the user
has to choose one option.

The general syntax is:


w = RadioButton(master, option=value)

• activebackground: to set the background color when widget is under the cursor.
• bg: to set he normal background color.
• command: to call a function.
• font: to set the font on the button label.
• image: to set the image on the widget.
• width: to set the width of the label in characters.
• height: to set the height of the label in characters.

Example

from tkinter import *


root = Tk()
v = IntVar()
Radiobutton(root, text=‘Male', variable=v, value=1).pack(anchor=W)
Radiobutton(root, text=‘Female', variable=v, value=2).pack(anchor=W)
mainloop()

Example-2 for Radio Button

from tkinter import *


def show():
v1=int(var1.get())
if v1==1 : msg.set( "You selected Chocolate")
if v1==2: msg.set( "You selected IceCream")
return
master = Tk()
var1 = IntVar()
msg=StringVar()
var1.set(2) # default selection
Radiobutton(master, text='Chocolate', variable=var1 , value=1).grid(row=1, sticky=W)
Radiobutton(master, text='IceCream', variable=var1, value=2).grid(row=2, sticky=W)
b1 = Button(master, text='Click Here', font='Arial', fg='blue', width=25, command=show)
b1.grid(row=3,columnspan=2)
Label(master, fg='red', font='Arial', textvariable=msg).grid(row=4 , column=1)
mainloop()
Frame

It acts as a container to hold the widgets. It is used for grouping and organizing the widgets.

Syntax :

w = Frame(master, option=value)

• master is the parameter used to represent the parent window.


• highlightcolor: To set the color of the focus highlight when widget has to be
focused.
• bd: to set the border width in pixels.
• bg: to set the normal background color.
• cursor: to set the cursor used.
• width: to set the width of the widget.
• height: to set the height of the widget.

Example

from tkinter import *


root = Tk()
frame = Frame(root)
frame.pack()
bottomframe = Frame(root)
bottomframe.pack( side = BOTTOM )
redbutton = Button(frame, text = 'Red', fg ='red')
redbutton.pack( side = LEFT)
greenbutton = Button(frame, text = 'Brown', fg='brown')
greenbutton.pack( side = LEFT )
bluebutton = Button(frame, text ='Blue', fg ='blue')
bluebutton.pack( side = LEFT )
blackbutton = Button(bottomframe, text ='Black', fg ='black')
blackbutton.pack( side = BOTTOM)
root.mainloop()

You might also like