changing a print script and turn it into a label script for GUI program in python 3.9

Hi there I want to take this print script, modify it as a label script and use it to print out the coordinates in my GUI Tkinter python program. I am using the SparkFun GPS-RTK Dead Reckoning pHAT GPS-16475. I have tried many ways but with no prevail. Please help me!

Thank you,

Gerard Faber.

import serial

from ublox_gps import UbloxGps

port = serial.Serial(‘/dev/serial0’, baudrate=38400, timeout=1)

gps = UbloxGps(port)

def run():

try:

print(“Listening for UBX Messages”)

while True:

try:

geo = gps.geo_coords()

print("Longitude: ", geo.lon)

print("Latitude: ", geo.lat)

print("Heading of Motion: ", geo.headMot)

except (ValueError, IOError) as err:

print(err)

finally:

port.close()

if name == ‘main’:

run()

This is what I have ended up with and it doesn’t work.

import serial

import tkinter as tk

from ublox_gps import UbloxGps

port = serial.Serial(‘/dev/serial0’, baudrate=38400, timeout=1)

gps = UbloxGps(port)

root = tk.Tk()

geo = gps.geo_coords()

def run():

label_lat = tk.Label(root, textvariable=geo.lat)

label_lat.pack

label_lon = tk.Label(root, textvariable=geo.lon)

label_lon.pack

port.close()

root.mainloop()

I tried that one too 6 months ago. I can’t use a WHILE method for it affects the rest of my GUI. It has to be an AFTER method for it to work with the GUI.

This is what I am working on now. But it is not reading GEO.LON and GEO.LAT.

FileNotFoundError: [Errno 2] No such file or directory; ‘geo.lon’.

import tkinter as tk

import serial

from ublox_gps import UbloxGps

port = serial.Serial(‘/dev/serial0’, baudrate=38400, timeout=1)

gps = UbloxGps(port)

def run():

geo = gps.geo_coords()

with open(“geo.lon”,“r”) as t:

lon_text.config(text=t.read())

with open(“geo.lat”,“r”) as s:

lat_text.config(text=s.read())

root.after(1000, loop)

root = tk.Tk()

root.title(“GPS”)

using 2 lines per label is very important to make this work!

lbl=tk.Label(text=“Longitude”)

lbl.grid(row=1, column=0)

time_text = tk.Label(root, text=‘loading’)

time_text.grid(row=1, column=1)

lbl=tk.Label(text=“Latitude”)

lbl.grid(row=2,column=0)

speed_text = tk.Label(root, text=‘loading’)

speed_text.grid(row=2, column=1)

exitButton=tk.Button(root, text=‘exit’, command=root.destroy)

exitButton.grid()

run() # start your loop

tk.mainloop() # start the mainloop