0% found this document useful (0 votes)
10 views15 pages

Python Tkinter GUI Development Guide

This document presents several practices for creating graphical interfaces with Python using Tkinter. The first practice shows how to create a basic window. The second adds a text label to the window. The third adds a button. The fourth creates a form to capture student data. The fifth practice calculates the days lived by a person based on their age. Each practice includes sample code and challenges to expand knowledge about Tkinter.

Translated by

ScribdTranslations
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)
10 views15 pages

Python Tkinter GUI Development Guide

This document presents several practices for creating graphical interfaces with Python using Tkinter. The first practice shows how to create a basic window. The second adds a text label to the window. The third adds a button. The fourth creates a form to capture student data. The fifth practice calculates the days lived by a person based on their age. Each practice includes sample code and challenges to expand knowledge about Tkinter.

Translated by

ScribdTranslations
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

Graphic Interface with Python

Python Tkinter
Creation of Graphical Interfaces

1
Graphical Interface with Python

Content
Basic Widgets 2
Worktca 01 First Window 2
Worktca 02 Hello World with Label 3
Worktca 03 Hello World with Buton 5
Worktca 04 Student form 6
Worktabout 5 Calculation of days lived 8

2
Graphical Interface with Python

Practice 01 First Window

Objective

Create a window with the programming language Python

Code

#Import TKinter library

import tkinter as tk

root = [Link]() #Create a new window

[Link]("Hello World") #Title

[Link]()

Challenge: Try the following commands and play with the properties of the root object

Commands Descripción Observations

[Link]("600x300") Size

[Link]("600x300+500+10") Size and position

root['bg']='green'

3
Graphic Interface with Python

Practice 02 Hello World with Label

Code

#Import Tkinter library

import tkinter as tk

root = [Link]() #Creates a new window

[Link]('600x300+500+10')

[Link]("Hello World") #Title

root['bg']='green'

widgetLabel = [Link](root, text="Hello World!")

[Link]()

[Link]()

4
Graphical Interface with Python

Desafoo

Play with the following commands through the root object

Add a new label

Commands Description Observations

widgetLabel['fg']='white'

widgetLabel['font']='Arial 24'

5
Graphical Interface with Python

Practice 03 Hello World with Button

Code

Importing graphic libraries

from tkinter import *

from tkinter import tk

root = Tk()

ventana =[Link](root, padding=20) #Crear una ventana

[Link]() #Grid for the components

#Label

Hello World!

Button

[Link](window, text="exit", command=[Link]).grid(column=1, row=0)

[Link]()

6
Graphical Interface with Python

Practice 04 Student Form

Object

Define a User Interface for capturing student information

Code

#Importing graphical libraries

from tkinter import *

root = Tk() #Creates a window

[Link]('500x300')

Student Capture Method

root['bg']='#ff9a52'

#Label1

labelGreetng = Label(root, text="Datos del estudiante")

[Link](x=100, y=20)

7
Graphical Interface with Python

labelGreetng['bg']='#ff9a52'

labelGreetng['fg']='#690202'

labelGreetng['font']= 'Arial 20'

#Label2

labelNombre = Label(root, text='Nombre')

labelNombre['font'] ='Roboto 12'

[Link](x=10, y=60)

Entry

entryNombre = Entry(root)

[Link](x=150, y=60)

#Button

buuonSalir = Buuon(root, text="Salir", command=[Link])

[Link](x=150, y=100)

buuonSalir['width'] = '15'

[Link]()

Dare

●Crear la siguiente interfaz agregando los controles

Customize the colors of the Widgets according to your tastes.

8
Graphical Interface with Python

9
Graphic Interface with Python

Practice 5 Calculation of days lived

Code
from tkinter import *

win = Tk()

[Link]("600x300")

Ways Days Lived

win['bg'] = '#9dc9ac'

# Título de Formulario

labelTitle = Label(win, text="Cálculo de Días Vividos")

labelTitle['font'] = 'Arial 20'

labelTitle['bg'] = '#45aab8'

labelTitle['fg'] = '#39324d'

[Link](x=150, y=5)

# Título de Label Nombre

labelEdad = Label(win, text="Ingresa la edad")

labelEdad['font'] = 'Arial 14'

labelEdad['bg'] = '#45aab8'

10
Graphical Interface with Python

labelEdad['fg'] = '#39324d'

[Link](x=20, y=60)

Control for age capture

edadEntry = Entry(win)

edadEntry['font'] = 'Arial 20'

edadEntry['bg'] = '#39324d'

edadEntry['fg'] = 'white'

[Link](x=200, y=60)

# Título de Label Edad

labelTotal = Label(win, text="Total")

labelTotal['font'] = 'Arial 14'

labelTotal['bg'] = '#45aab8'

labelTotal['fg'] = '#39324d'

[Link](x=20, y=100)

Age distribution

totalEntry = Entry(win, state=DISABLED)

totalEntry['font'] = 'Arial 20'

totalEntry['bg'] = '#39324d'

totalEntry['fg'] = 'white'

[Link](x=200, y=100)

Button to perform the calculations

botonCalcular = Buuon(win, text="Calcular")

botonCalcular['font'] = 'Arial 30'

botonCalcular['bg'] = '#39324d'

botonCalcular['fg'] = 'white'

[Link](x=200, y=150)

Exit Button

botonSalir = Buuon(win, text="Salir")

botonSalir['font'] = 'Arial 30'

botonSalir['bg'] = '#39324d'

11
Graphic Interface with Python

botonSalir['fg'] = 'white'

[Link](x=400, y=150)

[Link]()

Agregar las variables de control que ligan a las cajas de texto

Connect the control variables to the Entry components

Indicate to the button the function that will perform the calculations and set the value in control entryTotal.

12
Graphic Interface with Python

See result

Challenge to utilize error handling when incorrect data is entered, error messages

from tkinter import messagebox

def calculate():

try:

age = int([Link]()) * 365 # Extracts

[Link](str(age)) # Sets value

except ValueError:

13
Graphical Interface with Python

[Link](message="Error en el datos",ttle="Error")

totalEntry.focus_set()

14
Graphical Interface with Python

Cr

15

You might also like