0% found this document useful (0 votes)
3 views27 pages

Introduction to GUI

This document provides an overview of the Tkinter module in Python for creating graphical user interfaces (GUIs). It covers various widgets such as buttons, labels, entry fields, and layout management techniques like pack, grid, and place. Additionally, it includes examples of how to implement these widgets and manage their layouts in a GUI application.

Uploaded by

Saraswathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views27 pages

Introduction to GUI

This document provides an overview of the Tkinter module in Python for creating graphical user interfaces (GUIs). It covers various widgets such as buttons, labels, entry fields, and layout management techniques like pack, grid, and place. Additionally, it includes examples of how to implement these widgets and manage their layouts in a GUI application.

Uploaded by

Saraswathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Unit III

4rth Semester BCA


TVCC, Katpady
 GU Interface:
 The tkinter Module:
 Window and Widgets:
 Text, label,
 Button, Entry,
 List box, Check button,
 Radio button,
 Scrollbar, Spin box.
 Layout Management
 Pack, Grid and Place
 Python provides various options for developing
graphical user interfaces (GUIs). Most important are
listed below.
 Tkinter: Tkinter is the Python interface to the Tk GUI
toolkit shipped with Python.
Tkinter Programming
 Tkinter is the standard GUI library for Python.
 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 below-mentioned widgets to
the GUI application.
 Enter the main event loop to take action against each
event triggered by the user.
Example:

import tkinter
top = tkinter.Tk()
top.mainloop()
Tkinter Widgets
 Button : The Button widget is used to display buttons in your application.
 Canvas : The Canvas widget is used to draw shapes, such as lines, ovals,
polygons and rectangles, in your application.
 Checkbutton : The Check button widget is used to display a number of
options as checkboxes. The user can select multiple options at a time.
 Entry: The Entry widget is used to display a single-line text field for accepting
values from a user.
 Frame: The Frame widget is used as a container widget to organize other
widgets.
 Label: The Label widget is used to provide a single-line caption for other
widgets. It can also contain images.
 Listbox: The List box widget is used to provide a list of options to a user.
 Menubutton : The Menu button widget is used to display menus in your
application.
 Menu: The Menu widget is used to provide various commands to a user. These
 commands are contained inside Menu button.
 Message : The Message widget is used to display multiline text fields for
accepting values from a user.
 Radiobutton: The Radio button widget is used to display a number of options
as radio buttons. The user can select only one option at a time.
 Scale: The Scale widget is used to provide a slider widget.
 Scrollbar: The Scrollbar widget is used to add scrolling capability to various
widgets, such as list boxes.
 Text: The Text widget is used to display text in multiple lines.
 Toplevel: The Toplevel widget is used to provide a separate window container.
 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.
 PanedWindow: A Paned Window is a container widget that may contain any
number of panes,
 arranged horizontally or vertically.
 LabelFrame: A label frame is a simple container widget. Its primary purpose is
to act as a
 spacer or container for complex window layouts.
 tkMessageBox: This module is used to display message boxes in your
applications.
Frame:
 The Frame widget is very important for the
process of
 grouping and
 organizing other widgets.
 It works like a container, for arranging the position of
other widgets.
 It uses rectangular areas in the screen
 To organize the layout and to provide padding of these
widgets.
from tkinter import *
root = Tk()
frame =
Frame(root,cursor="dot",height=20,highlightthickness=10,highlightbackground="red")
frame.pack()

bottomframe = Frame(root,cursor="star")
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 = TOP)

bluebutton = Button(frame, text="Blue", fg="blue")


bluebutton.pack( side = BOTTOM )

blackbutton = Button(bottomframe,text="Black", fg="black")


blackbutton.pack( side = BOTTOM)

root.mainloop()
import tkinter as tk

def change_color():
colors = ["red", "green", "blue", "yellow", "purple"]
new_color = random.choice(colors)
frame.config(bg=new_color)

# Create the main window


root = tk.Tk()
root.title("Change Frame Color Example")

# Create a frame
frame = tk.Frame(root, width=200, height=150, bg="lightgray")
frame.pack(pady=20)

# Create a button to change the color


import random
change_button = tk.Button(root, text="Change Color", command=change_color)
change_button.pack()

# Run the main loop


root.mainloop()
Button:
 The Button widget is used to add buttons in a Python
application.
 These buttons can display text or images that convey
the purpose of the buttons
 Syntax:
w = Button ( master, option=value, ... )

 master: This represents the parent window.


 options: Here is the list of most commonly used
options for this widget
import tkinter
top = tkinter.Tk()

def save():
print( "data saved")

def donothing():
print("do nothing")

B1=tkinter.Button(top,text="save",command=save,bd=50,bg="blue",
fg="white",padx=5,pady=5,height=10)
B1.pack(side="left")

B2 = tkinter.Button(top,text="Cancle",command=donothing,bd=12,
bg="red",fg="white",padx=10,height=5)
B2.flash()
B2.pack(side="bottom")
Textbox:
 To edit a multiline text
 Format the way it has to be displayed
 Syntax:
w = Button ( master, option=value, ... )

 master: This represents the parent window.


 options: Here is the list of most commonly used
options for this widget
Methods:
 delete(startindex [,endindex]): This method deletes a
specific character or a range of text.
 get(startindex [,endindex]): This method returns a
specific character or a range of text.
 index(index): Returns the absolute value of an index
based on the given index.
 insert(index [,string]...): This method inserts strings
at the specified index location
 see(index): This method returns true if the text
located at the index position is visible.
from tkinter import *
root = Tk()
text = Text(root)
text.insert(INSERT, "Hello.....")
text.insert(END, "Bye Bye.....")
text.pack()
text.tag_add("here", "1.0", "1.4")
text.tag_add("start", "1.8", "1.13")
text.tag_config("here", background="yellow", foreground="blue")
text.tag_config("start", background="Gray", foreground="green")
root.mainloop()
Label:
 This widget implements a display box where you can
place text or images.
 Syntax:
w = Button ( master, option=value, ... )

 master: This represents the parent window.


 options: Here is the list of most commonly used
options for this widget
from tkinter import *
root = Tk()
var = "Hey!?\n How are you doing?"
label=Label( root, text=var, relief=RAISED,padx=25,
pady=25,bg="blue",bd=25,cursor="star",font="Arial",
underline=4)
label.pack()
root.mainloop()
Entry
 The Entry widget is used to accept single-line text strings
from a user.
 If you want to display multiple lines of text that can be
edited, then you should use the Text widget.
 If you want to display one or more lines of text that cannot
be modified by the user, then you should use the Label
widget.
 Syntax:
w = Button ( master, option=value, ... )

 master: This represents the parent window.


 options: Here is the list of most commonly used options for
this widget
from tkinter import * L1 = Label(top, text="User Name")
L1.grid(row=0,column=0,padx=25,pady=25)
import sqlite3
top = Tk() E1 = Entry(top, bd =10,bg="red",fg="white",font="arial")
E1.grid(row=0,column=1,padx=25,pady=25)
def onclick(): L2 = Label(top, text="Password")
uname=E1.get() L2.grid(row=1,column=0,padx=25,pady=25)
pword=E2.get()
E2 = Entry(top, bd =25,bg="green",fg="white",show="*",font="arial")
print(uname) E2.grid(row=1,column=1,padx=25,pady=25)
print(pword)
B1=Button(top,text="login",command=onclick)
B1.grid(row=2,column=0,padx=25,pady=25)
def dele():
print("not saved") B2=Button(top,text="cancle",command=dele)
B2.grid(row=2,column=1,padx=25,pady=25)

top.mainloop()
Checkbutton
 The Checkbutton widget is used to display a number of options to a
user as toggle buttons.

from tkinter import *


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=5, width = 20)
C1.pack()
C2.pack()
top.mainloop()
RadioButton
 Radio buttons allow you to select between one of
a number of mutually exclusive choices
Listbox
 The Listbox widget is used to display a list of items from which a user can select
a number of items
 Implementation:

from tkinter import *


top = Tk()
label = Label( top, text="subject:", relief=RAISED ).pack()
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(side=RIGHT,padx=20,pady=20)
top.mainloop()
Scroll bar:
 This widget provides a slide controller that is used to implement
vertical scrolled
 widgets, such as Listbox, Text and Canvas.
 Implementation:

from tkinter import *


root = Tk()
scrollbar = Scrollbar(root)
scrollbar.pack( side = RIGHT, fill=Y )
mylist = Listbox(root, yscrollcommand = scrollbar.set )
for line in range(100):
mylist.insert(END, "This is line number " + str(line))
mylist.pack( side = LEFT, fill = BOTH )
scrollbar.config( command = mylist.yview )
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.

 delete(startindex [,endindex]): This method deletes a specific


character or a range of text.
 get(startindex [,endindex]): This method returns a specific character or
a range of text.
 identify(x, y): Identifies the widget element at the given location.
 index(index):Returns the absolute value of an index based on the given
index.
 insert(index [,string]...): This method inserts strings at the specified
index location.
 invoke(element): Invokes a spinbox button.
Implementation:
from tkinter import *
master = Tk()
w = Spinbox(master, from_=0, to=10)
w.pack()
mainloop()
Layout management
 tkinter also offers access to the geometric
configuration of the widgets which can organize the
widgets in the parent windows.
 There are mainly three geometry manager classes.
 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.

You might also like