Prerrequisitos: Introducción a Tkinter | Uso de la API de array de distancia de Google
Python ofrece múltiples opciones para desarrollar GUI (interfaz gráfica de usuario). De todos los métodos GUI, tkinter es el método más utilizado. Es una interfaz estándar de Python para el kit de herramientas Tk GUI que se envía con Python. Python con tkinter genera la forma más rápida y sencilla de crear aplicaciones GUI. Crear una GUI usando tkinter es una tarea fácil.
Para crear un tkinter:
- Importando el módulo – tkinter
- Crear la ventana principal (contenedor)
- Agregue cualquier cantidad de widgets a la ventana principal
- Aplique el evento Trigger en los widgets.
Vamos a crear una calculadora de distancia-tiempo basada en GUI utilizando el módulo Python Tkinter, que puede indicar la distancia entre dos ciudades/ubicaciones y el tiempo necesario para viajar de una ubicación a otra.
Módulos requeridos:
tkinter requests json
A continuación se muestra la implementación:
# Python3 program to create Distance # Time GUI calculator using Tkinter # import everything from tkinter modules from tkinter import * # import modules import requests, json # Function for finding distance # and duration between two places def result(source, destination, travel_modes): # Enter your API key here api_key = 'Your_api_key' # base variable to store base url base = 'https://maps.googleapis.com/maps/api/distancematrix/json?' # Check travel modes if travel_modes == "train": # complete_url variable to store complete url address complete_url = base + 'origins =' + source + \ '&destinations =' + destination + \ '&mode = transit&transit_mode = train' + \ '&key ='+api_key # get method of requests module # return response object r = requests.get(complete_url) else: # complete_url variable to # store complete url address complete_url = base + 'origins =' + source+ \ '&destinations ='+ destination + \ '&mode ='+travel_modes+'&key ='+ api_key # get method of requests module # return response object r = requests.get(complete_url) # json method of response object convert # json format data into python format data x = r.json() # x contains list of nested dictionaries # we know dictionary contains key value pair # Extracting useful information # from x dictionary row = x['rows'][0] cell = row['elements'][0] # Check value corresponding to # status key in cell dictionary if cell['status'] == 'OK' : # insert method inserting the # value in the text entry box. # Extracting useful information # from cell dictionary and inserting # into the respective text fields. distance_field.insert(10, cell['distance']['text']) duration_field.insert(10, cell['duration']['text']) else : # insert method inserting the # value in the text entry box. # Extract value corresponding to # status key from cell dictionary and # inserting into the respective text fields. mode_field.insert(10, cell['status']) distance_field.insert(10, cell['status']) # Function for getting values from # respective text entry boxes and # calling result function . def find() : # get method returns current text # as a string from text entry box source = source_field.get() destination = destination_field.get() travel_modes = mode_field.get() # Calling result() Function result(source, destination, travel_modes) # Function for inserting the train string # in the mode_field text entry box def train() : mode_field.insert(10, "train") # Function for inserting the driving string # in the mode_field text entry box def driving() : mode_field.insert(10, "driving") # Function for inserting the walking string # in the mode_field text entry box def walking() : mode_field.insert(10, "walking") # Function for clearing the contents # of source_field, distance_field, # duration_field text entry boxes. def del_source() : source_field.delete(0, END) distance_field.delete(0, END) duration_field.delete(0, END) # Function for clearing the contents of # destination_field, distance_field, # duration_field text entry boxes. def del_destination() : destination_field.delete(0, END) distance_field.delete(0, END) duration_field.delete(0, END) # function for clearing the contents of mode_field, # distance_field, duration_field text entry boxes. def del_modes() : mode_field.delete(0, END) distance_field.delete(0, END) duration_field.delete(0, END) # Function for clearing the # contents of all text entry boxes def delete_all() : source_field.delete(0, END) destination_field.delete(0, END) mode_field.delete(0, END) distance_field.delete(0, END) duration_field.delete(0, END) # Driver code if __name__ == "__main__" : # Create a GUI window root = Tk() # Set the background colour of GUI window root.configure(background = 'light green') # Set the configuration of GUI window root.geometry("500x300") # Create a welcome to distance time calculator label headlabel = Label(root, text = 'welcome to distance time calculator', fg = 'black', bg = "red") # Create a Source: label label1 = Label(root, text = "Source:", fg = 'black', bg = 'dark green') # Create a Destination: label label2 = Label(root, text = "Destination:", fg = 'black', bg = 'dark green') # Create a Choose travelling modes: label label3 = Label(root, text = "Choose travelling modes: ", fg = 'black', bg = 'red') # Create a Distance: label label4 = Label(root, text = "Distance:", fg = 'black', bg = 'dark green') # Create a Duration: label label5 = Label(root, text = "Duration:", fg = 'black', bg = 'dark green') # grid method is used for placing # the widgets at respective positions # in table like structure . headlabel.grid(row = 0, column = 1) label1.grid(row = 1, column = 0, sticky ="E") label2.grid(row = 2, column = 0, sticky ="E") label3.grid(row = 3, column = 1) label4.grid(row = 7, column = 0, sticky ="E") label5.grid(row = 8, column = 0, sticky ="E") # Create a text entry box # for filling or typing the information. source_field = Entry(root) destination_field = Entry(root) mode_field = Entry(root) distance_field = Entry(root) duration_field = Entry(root) # grid method is used for placing # the widgets at respective positions # in table like structure . # ipadx keyword argument set width of entry space . source_field.grid(row = 1, column = 1, ipadx ="100") destination_field.grid(row = 2, column = 1, ipadx ="100") mode_field.grid(row = 5, column = 1, ipadx ="50") distance_field.grid(row = 7, column = 1, ipadx ="100") duration_field.grid(row = 8, column = 1, ipadx ="100") # Create a CLEAR Button and attached # to del_source function button1 = Button(root, text = "CLEAR", bg = "red", fg = "black", command = del_source) # Create a CLEAR Button and attached to del_destination button2 = Button(root, text = "CLEAR", bg = "red", fg = "black", command = del_destination) # Create a RESULT Button and attached to find function button3 = Button(root, text = "RESULT", bg = "red", fg = "black", command = find) # Create a CLEAR ALL Button and attached to delete_all function button4 = Button(root, text = "CLEAR ALL", bg = "red", fg = "black", command = delete_all) # Create a Train Button and attached to train function button5 = Button(root, text = "Train", command = train) # Create a Driving Button and attached to driving function button6 = Button(root, text = "Driving", command = driving) # Create a Walking Button and attached to walking function button7 = Button(root, text = "Walking", command = walking) # Create a CLEAR Button and attached to del_modes function button8 = Button(root, text = "CLEAR", fg = "black", bg = "red", command = del_modes) # grid method is used for placing # the widgets at respective positions # in table like structure . button1.grid(row = 1, column = 2) button2.grid(row = 2, column = 2) button3.grid(row = 6, column = 1) button4.grid(row = 9, column = 1) button5.grid(row = 4, column = 0) button6.grid(row = 4, column = 1) button7.grid(row = 4, column = 2) button8.grid(row = 5, column = 2) # Start the GUI root.mainloop()
Producción: