Python Tkinter PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Ceh.arvind@gmail.

com Python -Classes 9996870071

Python Tkinter
Python provides the standard library Tkinter for creating the graphical user interface for desktop based
applications.

Developing desktop based applications with python Tkinter is not a complex task. An empty Tkinter top-
level window can be created by using the following steps.

1. import the Tkinter module.


2. Create the main application window.
3. Add the widgets like labels, buttons, frames, etc. to the window.
4. Call the main event loop so that the actions can take place on the user's computer screen.

Example

# !/usr/bin/python3
from tkinter import *
#creating the application main window.
top = Tk()
#Entering the event main loop
top.mainloop()
Output:

Skill_Centre Ambala Cantt-01 Page-1


[email protected] Python -Classes 9996870071
Tkinter widgets
There are various widgets like button, canvas, checkbutton, entry, etc. that are used to build the python
GUI applications.

SN Widget Description

1 Button The Button is used to add


various kinds of buttons to the
python application.

2 Checkbutton The Checkbutton is used to


display the CheckButton on the
window.

3 Entry The entry widget is used to


display the single-line text field
to the user. It is commonly used
to accept user values.

4 Label A label is a text used to display


some message or information
about the other widgets.

5 Radiobutton The Radiobutton is different


from a checkbutton. Here, the
user is provided with various
options and the user can select
only one option among them.

6 MessageBox This module is used to display


the message-box in the desktop
based applications.

Skill_Centre Ambala Cantt-01 Page-2


[email protected] Python -Classes 9996870071
Python Tkinter Geometry
The Tkinter geometry specifies the method by using which, the widgets are represented on display. The
python Tkinter provides the following geometry methods.

1. The pack() method


2. The grid() method
3. The place() method

Python Tkinter pack() method


The pack() widget is used to organize widget in the block. The positions widgets added to the python
application using the pack() method can be controlled by using the various options specified in the method
call.

However, the controls are less and widgets are generally added in the less organized manner.

Example

# !/usr/bin/python3
from tkinter import *
parent = Tk()
redbutton = Button(parent, text = "Red", fg = "red")
redbutton.pack( side = LEFT)
greenbutton = Button(parent, text = "Black", fg = "black")
greenbutton.pack( side = RIGHT )
bluebutton = Button(parent, text = "Blue", fg = "blue")
bluebutton.pack( side = TOP )
blackbutton = Button(parent, text = "Green", fg = "red")
blackbutton.pack( side = BOTTOM)
parent.mainloop()

Output:

Skill_Centre Ambala Cantt-01 Page-3


[email protected] Python -Classes 9996870071
Python Tkinter grid() method
The grid() geometry manager organizes the widgets in the tabular form. We can specify the rows and
columns as the options in the method call. We can also specify the column span (width) or
rowspan(height) of a widget.

A list of possible options that can be passed inside the grid() method is given below.

o Column
The column number in which the widget is to be placed. The leftmost column is represented by 0.
o row
The row number in which the widget is to be placed. The topmost row is represented by 0.

Example

# !/usr/bin/python3
from tkinter import *
parent = Tk()
name = Label(parent,text = "Name").grid(row = 0, column = 0)
e1 = Entry(parent).grid(row = 0, column = 1)
password = Label(parent,text = "Password").grid(row = 1, column = 0
)
e2 = Entry(parent).grid(row = 1, column = 1)
submit = Button(parent, text = "Submit").grid(row = 4, column = 0)
parent.mainloop()

Output:

Skill_Centre Ambala Cantt-01 Page-4


[email protected] Python -Classes 9996870071
Python Tkinter place() method
The place() geometry manager organizes the widgets to the specific x and y coordinates.

Example

# !/usr/bin/python3
from tkinter import *
top = Tk()
top.geometry("400x250")
name = Label(top, text = "Name").place(x = 30,y = 50)
email = Label(top, text = "Email").place(x = 30, y = 90)
password = Label(top, text = "Password").place(x = 30, y = 130)
e1 = Entry(top).place(x = 80, y = 50)
e2 = Entry(top).place(x = 80, y = 90)
e3 = Entry(top).place(x = 95, y = 130)
top.mainloop()

Output:

Skill_Centre Ambala Cantt-01 Page-5


[email protected] Python -Classes 9996870071
Python Tkinter Button
The button widget is used to add various types of buttons to the python application. Python allows us to
configure the look of the button according to our requirements. Various options can be set or reset
depending upon the requirements.

We can also associate a method or function with a button which is called when the button is pressed.

Example

#python application to create a simple button

from tkinter import *


top = Tk()
top.geometry("200x100")
b = Button(top,text = "Simple")
b.pack()
top.mainaloop()

Output:

Syntax
1. W = Button(parent, options)

1 activebackground It represents the background of the button


when the mouse hover the button.

2 activeforeground It represents the font color of the button when


the mouse hover the button.

3 Padx Additional padding to the button in the


horizontal direction.

4 pady Additional padding to the button in the vertical


direction.

Skill_Centre Ambala Cantt-01 Page-6


[email protected] Python -Classes 9996870071
Example

from tkinter import *

top = Tk()

top.geometry("200x100")

def fun():
messagebox.showinfo("Hello", "Red Button clicked")

b1 = Button(top,text = "Red",command = fun,activeforeground = "red",a


ctivebackground = "pink",pady=10)

b2 = Button(top, text = "Blue",activeforeground = "blue",activebackgrou


nd = "pink",pady=10)

b3 = Button(top, text = "Green",activeforeground = "green",activebackgr


ound = "pink",pady = 10)

b4 = Button(top, text = "Yellow",activeforeground = "yellow",activeback


ground = "pink",pady = 10)

b1.pack(side = LEFT)

b2.pack(side = RIGHT)

b3.pack(side = TOP)

b4.pack(side = BOTTOM)

top.mainloop()

Output:

Skill_Centre Ambala Cantt-01 Page-7

You might also like