graphics
graphics
graphics
Out
of all the GUI methods, tkinter is the most commonly used method. It is a standard
Python interface to the Tk GUI toolkit shipped with Python. Python tkinter is the
fastest and easiest way to create GUI applications. Creating a GUI using tkinter is
an easy task.
import tkinter
There are two main methods used which the user needs to remember while creating the
Python application with GUI.
mainloop(): There is a method known by the name mainloop() is used when your
application is ready to run. mainloop() is an infinite loop used to run the
application, wait for an event to occur and process the event as long as the window
is not closed.
m.mainloop()
import tkinter
m = tkinter.Tk()
'''
widgets are added here
'''
m.mainloop()
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 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.
There are a number of widgets which you can put in your tkinter application. Some
of the major widgets are explained below:
Button:To add a button in your application, this widget is used. The general syntax
is:
w=Button(master, option=value)
master is the parameter used to represent the parent window. There are number of
options which are used to change the format of the Buttons. Number of options can
be passed as parameters separated by commas. Some of them are listed below.
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 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.
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()
Output:
Canvas: It is used to draw pictures and other complex layout like graphics, text
and widgets. The general syntax is:
w = Canvas(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of the widget.
Number of options can be passed as parameters separated by commas. Some of them are
listed below.
Output:
CheckButton: To select any number of options by displaying a number of options to a
user as toggle buttons. The general syntax is:
w = CheckButton(master, option=value)
There are number of options which are used to change the format of this widget.
Number of options can be passed as parameters separated by commas. Some of them are
listed below.
Output:
Entry:It is used to input the single line text entry from the user.. For multi-line
text input, Text widget is used. The general syntax is:
w=Entry(master, option=value)
master is the parameter used to represent the parent window. There are number of
options which are used to change the format of the widget. Number of options can be
passed as parameters separated by commas. Some of them are listed below.
Output:
Frame: It acts as a container to hold the widgets. It is used for grouping and
organizing the widgets. The general syntax is:
w = Frame(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of the widget.
Number of options can be passed as parameters separated by commas. Some of them are
listed below.
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.
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()
Output:
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. The general syntax is:
w=Label(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of the widget.
Number of options can be passed as parameters separated by commas. Some of them are
listed below.
Output:
Listbox: It offers a list to the user from which the user can accept any number of
options. The general syntax is:
w = Listbox(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of the widget.
Number of options can be passed as parameters separated by commas. Some of them are
listed below.
highlightcolor: To set the color of the focus highlight when widget has to be
focused.
bg: to set the normal background color.
bd: to set the border width in pixels.
font: to set the font on the button label.
image: to set the image on the widget.
width: to set the width of the widget.
height: to set the height of the widget.
top = Tk()
Lb = Listbox(top)
Lb.insert(1, 'Python')
Lb.insert(2, 'Java')
Lb.insert(3, 'C++')
Lb.insert(4, 'Any other')
Lb.pack()
top.mainloop()
Output:
MenuButton: It is a part of top-down menu which stays on the window all the time.
Every menubutton has its own functionality. The general syntax is:
w = MenuButton(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of the widget.
Number of options can be passed as parameters separated by commas. Some of them are
listed below.
top = Tk()
mb = Menubutton ( top, text = "GfG")
mb.grid()
mb.menu = Menu ( mb, tearoff = 0 )
mb["menu"] = mb.menu
cVar = IntVar()
aVar = IntVar()
mb.menu.add_checkbutton ( label ='Contact', variable = cVar )
mb.menu.add_checkbutton ( label = 'About', variable = aVar )
mb.pack()
top.mainloop()
Output:
Menu: It is used to create all kinds of menus used by the application. The general
syntax is:
w = Menu(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of this widget.
Number of options can be passed as parameters separated by commas. Some of them are
listed below.
root = Tk()
menu = Menu(root)
root.config(menu=menu)
filemenu = Menu(menu)
menu.add_cascade(label='File', menu=filemenu)
filemenu.add_command(label='New')
filemenu.add_command(label='Open...')
filemenu.add_separator()
filemenu.add_command(label='Exit', command=root.quit)
helpmenu = Menu(menu)
menu.add_cascade(label='Help', menu=helpmenu)
helpmenu.add_command(label='About')
mainloop()
Output:
Message: It refers to the multi-line and non-editable text. It works same as that
of Label. The general syntax is:
w = Message(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of the widget.
Number of options can be passed as parameters separated by commas. Some of them are
listed below.
Output:
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)
There are number of options which are used to change the format of this widget.
Number of options can be passed as parameters separated by commas. Some of them are
listed below.
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 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 widget.
width: to set the width of the label in characters.
height: to set the height of the label in characters.
Output:
Scale: It is used to provide a graphical slider that allows to select any value
from that scale. The general syntax is:
w = Scale(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of the widget.
Number of options can be passed as parameters separated by commas. Some of them are
listed below.
cursor: To change the cursor pattern when the mouse is over the widget.
activebackground: To set the background of the widget when mouse is over the
widget.
bg: to set the normal background color.
orient: Set it to HORIZONTAL or VERTICAL according to the requirement.
from_: To set the value of one end of the scale range.
to: To set the value of the other end of the scale range.
image: to set the image on the widget.
width: to set the width of the widget.
Output:
Scrollbar: It refers to the slide controller which will be used to implement listed
widgets. The general syntax is:
w = Scrollbar(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of the widget.
Number of options can be passed as parameters separated by commas. Some of them are
listed below.
highlightcolor: To set the color of the focus highlight when widget has to be
focused.
insertbackground: To set the background of the widget.
bg: to set the normal background color.
font: to set the font on the button label.
image: to set the image on the widget.
width: to set the width of the widget.
height: to set the height of the widget.
Output:
TopLevel: This widget is directly controlled by the window manager. It don’t need
any parent window to work on.The general syntax is:
w = TopLevel(master, option=value)
There are number of options which are used to change the format of the widget.
Number of options can be passed as parameters separated by commas. Some of them are
listed below.
Output:
SpinBox: It is an entry of ‘Entry’ widget. Here, value can be input by selecting a
fixed value of numbers.The general syntax is:
w = SpinBox(master, option=value)
There are number of options which are used to change the format of the widget.
Number of options can be passed as parameters separated by commas. Some of them are
listed below.
Output:
PannedWindowIt is a container widget which is used to handle number of panes
arranged in it. The general syntax is:
w = PannedWindow(master, option=value)
master is the parameter used to represent the parent window. There are number of
options which are used to change the format of the widget. Number of options can be
passed as parameters separated by commas. Some of them are listed below.