0% found this document useful (0 votes)
18 views5 pages

Basic Tkinter Tutorial

This document presents a basic tutorial on using the Tkinter module in Python to create graphical user interfaces. It explains how to create windows, labels, text boxes, buttons, drop-down lists, and more. Additionally, it shows how to manipulate these elements and add functionality using methods such as pack, place, grid, and button events.
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)
18 views5 pages

Basic Tkinter Tutorial

This document presents a basic tutorial on using the Tkinter module in Python to create graphical user interfaces. It explains how to create windows, labels, text boxes, buttons, drop-down lists, and more. Additionally, it shows how to manipulate these elements and add functionality using methods such as pack, place, grid, and button events.
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

Basic Python Course

Tkinter Handling Tutorial


We will start with the code for a window. This code is the basics for
instantiate a window

We import the tkinter module


from Tkinter import *
We create a window
window = Tk()
We show the window
[Link]()

To manipulate the window its size and position on the screen we use the
geometry method and to give it a title the title method.

We import the tkinter module


from Tkinter import *
#We create a window
window = Tk()
Assign a size to the window
geometry('width X height + posx + posy'
[Link]('500x300+100+100')
#We assign a title to the window
Window with Labels
We initialize the procedure
It is done when we have finished creating everything.
the widgets
[Link]()

We will continue adding window manipulation code as time goes by.


tutorial.

Label
In the following code we will see how to create a label and place it in the window with
the pack method but the problem is that they are placed automatically.

from Tkinter import *


window = Tk()
[Link]('500x300+100+100')
[Link]('Window with Labels
(Labels)
#We create the labels
User
Giancarlo Escobedo
We add the label lblUsuario to the
window
[Link]()
[Link]()
[Link]()

Giancarlo Escobedo Valdivia


Basic Python Course

In order to give it a precise location in the window, we will use another method.
called place instead of pack. Care must be taken as the measurements that
we use it in pixels and not in characters so we have to keep testing
until reaching the desired location.

from Tkinter import *


window = Tk()
[Link]('500x300+100+100')
Window with Labels
#We create the labels
lblUsuario
User
Label(text='Giancarlo
Escobedo').place(x=210,y=20)
[Link]()

Another way to position the labels is with the grib method. It is necessary to have
we will use the window as a grid. We will also use a
function to change the color and fill of the label and the sticky function for the
location of the text.

from Tkinter import *


window = Tk()
[Link]('500x300+100+100')
Window with Labels
We create the labels
#fg is the foreground color of the text
#bg is background fill color
lblUsuario = Label(text='Usuario',fg='blue',bg
'yellow').grid(row=0,column=0)
Data Label
GianEsco
=W)
[Link]()

Text fields
For the text fields, we will need to create a variable of type StrinrVar()
to use the text field method, we will use the word entry to create
text fields, the text field has a size of 20 characters by
defect for which with the width property we can modify it, we use the
set method to input text.

#we import the tkinter module


from Tkinter import *
window = Tk()
[Link]('500x300+100+100')
[Link]('Window with Text Box')
We create the font tags
lblUsuario
User

Giancarlo Escobedo Valdivia


Basic Python Course

lblNombre = Label(text='Name',font=('Agency'
FB',14)).place(x=10,y=50)
We create text fields
StringVar()
txtUsuario
Entry(window, textvariable=inputU).place(x=100, y=20)
inputN = StringVar()
txtName = Entry(window, textvariable=inputN, width =
30).place(x=100,y=60)
[Link]('LP0001')
Giancarlo Escobedo Valdivia
[Link]()

Buttons
For the creation of Buttons, we will use the Button method.

we import the tkinter module


from Tkinter import *
window = Tk()
[Link]('500x300+100+100')
Window with Text Box
#We create the font tags
lblUsuario = Label(text='Usuario',font=('Verdana',14)).place(x=10,y=10)
Name
#We create text fields
entradaU = StringVar()
userTxt = Entry(window, textvariable=inputU).place(x=100, y=20)
inputN = StringVar()
txtName = Entry(window, textvariable=inputN, width =
30).place(x=100,y=60)
We Create Buttons
Button(window, text='Greet', font=('Arial', 12), width=
15).place(x=300,y=20)
Button(window, text='Dismiss', font=('Arial', 12), width=
15).place(x=300,y=60)
[Link]()

By adding events to the buttons, we will use the command property of the
next way:
We created two methods to help us.

we import the tkinter module


from Tkinter import *
Method responsible for Greeting the User
def Greet():
Hello
font = ('Calibri', 14)).place(x=10, y=150)
#Method responsible for Firing the Name
def Goodbye():

Giancarlo Escobedo Valdivia


Basic Python Course

Goodbye
font = ('Calibri',14)).place(x=10, y=180)

window = Tk()
[Link]('500x300+100+100')
Event Window with Buttons
We create the font tags
lblUsuario = Label(text='Usuario',font=('Verdana',14)).place(x=10,y=10)
Label(text='Name',font=('Agency FB',14)).place(x=10,y=50)
We create text fields
entradaU = StringVar()
txtUser = Entry(window,textvariable=inputU).place(x=100,y=20)
entradaN = StringVar()
txtName = Entry(window, textvariable=inputN, width =
30).place(x=100,y=60)
#We Create Buttons
Button(window, text='Greet', command=Greet,
font=('Arial',12),width = 15).place(x=300,y=20)
Button(window, text='Dismiss', command=Dismiss)
font=('Arial',12),width = 15).place(x=300,y=60)
[Link]()

List box
We will create a list box with some data.

We import the tkinter module


from Tkinter import *
window = Tk()
[Link]('700x600+0+0')
Window with Lists
lblMaterias
Label(ventana,text="Materia:").place(x=100,y=100)
#we create a List
Listbox(window, width=50)
[Link](0, 'Basic Programming')
[Link](1,'Object Oriented Programming')
[Link](2,'Operating Systems')
[Link](3,'Programming in Python')
Remove elements from the list
[Link](1)
[Link](x=100,y=120)
[Link]()

We added a label, text box, and button to input data to my list.

import the tkinter module


from Tkinter import *
We create the function to add
def add():
[Link](END,[Link]())

window = Tk()

Giancarlo Escobedo Valdivia


Basic Python Course

[Link]('700x600+0+0')
[Link]('Window with Lists')
Subject:
#we create a List
lstSubjects = Listbox(window, width=50)
[Link](0,'Basic Programming')
[Link](1, 'Object Oriented Programming')
[Link](2, 'Operating Systems')
[Link](3,'Programming in Python')
[Link](x=100,y=120)
We create a label, text box, and button
Label(window, text="Subjects:").place(x=100, y=20)
entradaL = StringVar()
txtMateria
Entry(window, textvariable=inputL, width=40).place(x=150, y=20)
add
add

[Link]()

Combo box

We import the tkinter module


from Tkinter import *
We create the function to add
window = Tk()
[Link]('600x600+0+0')
[Link]('Window with Combos')
lablCalifT=Label(window,text='grades in text:').place(x=20,y=100)
We create a combo
cobCalifT = Spinbox(window, values=('Failed', 'Passed', 'Substitute')
'NCP')).place(x=200,y=100)

Giancarlo Escobedo Valdivia

You might also like