Unit5_Tkinter (1)
Unit5_Tkinter (1)
Python provides various options for developing graphical user interfaces
(GUIs). Most important are listed below.
Tkinter: It is the easiest among all to get started with. It is Python’s
standard GUI (Graphical User Interface) package. It is the most
commonly used toolkit for GUI Programming in Python.
JPython: It is the Python platform for Java that is providing Python
scripts seamless access o Java class Libraries for the local machine.
wxPython: It is open-source cross-platform GUI toolkit written in C++. It
one of the alternatives to Tkinter, which is bundled with Python.
There are many other interfaces available for GUI. But these are the
most commonly used ones.
Python GUI Programming - Tkinter
It is the standard GUI toolkit for Python.
It was written by Fredrik Lundh.
Python when combined with 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.
Tkinter is an acronym for "Tk interface".
Note: Tkinter comes pre-installed with Python3, and you need not bother
about installing it.
Python GUI Programming - Tkinter
Install Tkinter
To see if you have Tkinter, launch python; then at the Python prompt,
type:
import Tkinter
Or in Python 3: import tkinter
There is method used you the user need to remember while creating the
Python application with GUI.
The basic code used to create the main window of the application is:
mainloop():
There is a method known by the name mainloop() is used when you are
ready for the application to run.
mainloop() is an infinite loop used to run the application, wait for an
event to occur and process the event till the window is not closed.
m.mainloop()
Example:
import tkinter
m = tkinter.Tk()
m.mainloop()
Python GUI Programming - Tkinter
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.
Pack() method
root = Tk()
mainloop()
Pack() method
Fill:
If you want to make the widgets as wide as the parent widget, you have to
use the fill=X option:
root = Tk()
w = Label(root, text="Red Sun", bg="red", fg="white")
w.pack(fill=X)
w = Label(root, text="Green Grass", bg="green", fg="black")
w.pack(fill=X)
w = Label(root, text="Blue Sky", bg="blue", fg="white")
w.pack(fill=X)
mainloop()
Pack() method
Padding:
The pack() manager knows four padding options, i.e. internal and
external padding and padding in x and y direction:
root = Tk()
mainloop()
Place() method
Syntax:
widget.place( place_options )
relx, rely − Horizontal and vertical offset as a float between 0.0 and
1.0, as a fraction of the height and width of the parent widget.
Example
top = Tk()
def helloCallBack():
messagebox.showinfo("Say Hello", "Hello World")
colours = ['red','green','orange','white','yellow','blue']
r=0
for c in colours:
Label(text=c, relief=RIDGE,width=15).grid(row=r,column=0)
Entry(bg=c, relief=SUNKEN,width=10).grid(row=r,column=1)
r=r+1
mainloop()
Tkinter Variable Classes
Variables can be used with most entry widgets to track changes to the
entered value.
The Checkbutton and Radiobutton widgets require variables to work
properly.
Some widgets (like text entry widgets, radio buttons and so on) can be
connected directly to application variables by using special options:
variable, textvariable, onvalue, offvalue, and value.
Create a Tkinter variable:
x = StringVar() # Holds a string; default value ""
x = IntVar() # Holds an integer; default value 0
x = DoubleVar() # Holds a float; default value 0.0
x = BooleanVar() # Holds a boolean, returns 0 for False and 1 for True
Example:
var = StringVar()
var.set("hello") #to set the value, call the set method
Tkinter Widgets
Tkinter provides various controls, such as buttons, labels and text boxes
used in a GUI application.
These controls are commonly called widgets.
Label
Button
tk_optoinMenu
Canvas
progress-bar
combo-box
radio button
frame
scroll bar
level
Separator
check-button
tree-view and many more...
entry
level-frame
menu
list – box
menu button
message
Tkinter Widgets - Label
Label: A label is a widget that displays text or images, typically that the
user will just view does not interact with. Labels are used for such things
as identifying controls or other parts of the user interface, providing
textual feedback or results, etc.
Syntax:
w = Label ( master, option, ... )
master − represents the parent window.
options − list of commonly used options for this widget.
Tkinter Widgets - Label
Tkinter Widgets - Button
Syntax:
w = Button ( master, option=value, ... )
master − represents the parent window.
options − list of commonly used options for this widget.
Tkinter Widgets - Button
Tkinter Widgets - Button
Tkinter Widgets - CheckButton
Checkbutton:
The Checkbutton widget is used to display a number of options to a user
as toggle buttons.
The user can then select one or more options by clicking the button
corresponding to each option.
Checkboxes are shown on the screen as square boxes.
A Checkbox has two states: on or off.
Syntax:
w = Checkbutton ( master, option, ... )
master − represents the parent window.
options − list of commonly used options for this widget.
Tkinter Widgets – Radio Button
Radiobutton:
A radio button, sometimes called option button, is a graphical user
interface element of Tkinter, which allows the user to choose (exactly) one
of a predefined set of options.
Syntax:
w = Radiobutton ( master, option, ... )
master − represents the parent window.
options − list of commonly used options for this widget.
Tkinter Widgets – Entry
Entry:
The Entry widget is used to accept single-line text strings from a user.
Entry widgets are the basic widgets of Tkinter used to get input, i.e. text
strings, from the user of an application.