Python Forum
TKinter Not Executing
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TKinter Not Executing
#1
Hi all... I am not sure why, but this code is no longer executing the GUI like it once did. The code runs fine, the 'Print' line shows up fine in the terminal, the data gets logged from the sensor in the text file, but I get no Tkinter window? Please help.... Not sure where I am going wrong.

from tkinter import *
import time
import datetime
import bme280
import smbus2
from smbus2 import SMBus
from smbus import SMBus
from bme280 import BME280


root = Tk()
root.title('Humidity Logger')
root.configure(background='white')
root.geometry("500x300")

bus = SMBus(1)
bme280 = BME280(i2c_dev=bus)

bme_label = Label(root, text = "bme280", fg = "green", bg = "white", font = ("Helvetica", 32))
bme_label.pack(pady = 35)

while True:
    now = datetime.datetime.now()
    temperature = bme280.get_temperature()
    humidity = bme280.get_humidity()
    pressure = bme280.get_pressure()
    tempf = float(temperature * 9.0 / 5.0) + 32.0

    print(now.strftime("%m-%d-%Y %H:%M:%S") + ' | Temperature(*F/*C): {:.2f}*F | {:05.2f}*C | Humidity: {:05.2f}% | Pressure: {:05.2f}hPa'.format(tempf,temperature,humidity,pressure))
    bme_label.configure(text='Temp(*F): {:.2f}*F | Humidity: {:05.2f}%'.format(tempf,temperature,humidity))

    FileName = str("/home/pi/BME280_DataLog.txt")
    with open(FileName, "a") as f:
        f.write(now.strftime("%m-%d-%Y %H:%M:%S") + ' | Temperature(*F/*C): {:.2f}*F | {:05.2f}*C | Humidity: {:05.2f}% | Pressure: {:05.2f}hPa'.format(tempf,temperature,humidity,pressure) + '\n')

    time.sleep(10)

root.mainloop()
Reply
#2
The line root.mainloop() is never reached because the code goes into an infinite loop from the while True: up to the time.sleep(10)

If the GUI was to work with this code the time.sleep would freeze the GUI because mainloop would not be able to update the GUI until the sleep finishes.

To get around this rather than using time.sleep, tkinter has an after method that will call a function after a number of milliseconds.
Here is the code with the addition of after but I can't test it as I don't have the bme280, smbus2 etc
import tkinter as tk
import datetime
import bme280
import smbus2
from smbus2 import SMBus
from smbus import SMBus
from bme280 import BME280
 
 
root = tk.Tk()
root.title('Humidity Logger')
root.configure(background='white')
root.geometry("500x300")
 
bus = SMBus(1)
bme280 = BME280(i2c_dev=bus)
 
bme_label = tk.Label(root, text = "bme280", fg = "green", bg = "white", font = ("Helvetica", 32))
bme_label.pack(pady = 35)
 
def update():
    now = datetime.datetime.now()
    temperature = bme280.get_temperature()
    humidity = bme280.get_humidity()
    pressure = bme280.get_pressure()
    tempf = float(temperature * 9.0 / 5.0) + 32.0
 
    print(now.strftime("%m-%d-%Y %H:%M:%S") + ' | Temperature(*F/*C): {:.2f}*F | {:05.2f}*C | Humidity: {:05.2f}% | Pressure: {:05.2f}hPa'.format(tempf,temperature,humidity,pressure))
    bme_label.configure(text='Temp(*F): {:.2f}*F | Humidity: {:05.2f}%'.format(tempf,temperature,humidity))
 
    FileName = str("/home/pi/BME280_DataLog.txt")
    with open(FileName, "a") as f:
        f.write(now.strftime("%m-%d-%Y %H:%M:%S") + ' | Temperature(*F/*C): {:.2f}*F | {:05.2f}*C | Humidity: {:05.2f}% | Pressure: {:05.2f}hPa'.format(tempf,temperature,humidity,pressure) + '\n')
 
    root.after(10000, update)
update()
root.mainloop()

Note if the following lines
temperature = bme280.get_temperature()
humidity = bme280.get_humidity()
pressure = bme280.get_pressure()
take a reasonable amount of time this will still freeze the GUI and will need to be done in a separate thread, see the link below for information.

The following thread has information on not freezing the GUI:
[Tkinter] How to deal with code that blocks the mainloop, freezing the gui
Nu2Python likes this post
Reply
#3
Smile 
Yoriz... thanks so much for the great explanation on this. I am still quite new with Python, so this is a great learning experience. This seems to be working perfectly and I do not notice any freezing yet with the GUI. Thanks again, very much appreciated! Smile

(Jan-05-2023, 11:42 PM)Yoriz Wrote: The line root.mainloop() is never reached because the code goes into an infinite loop from the while True: up to the time.sleep(10)

If the GUI was to work with this code the time.sleep would freeze the GUI because mainloop would not be able to update the GUI until the sleep finishes.

To get around this rather than using time.sleep, tkinter has an after method that will call a function after a number of milliseconds.
Here is the code with the addition of after but I can't test it as I don't have the bme280, smbus2 etc
Reply
#4
Hi there,

I'm sorry to hear that you're having trouble with your Python code. It sounds like you're having some difficulty understanding the syntax and structure of the language.

The best way to get help with Python is to ask questions on a forum like this one. There are many experienced Python users here who can help you out. You can also search online for tutorials and resources that can help you learn the basics of Python.

If you need more specific help, you can also hire a Python tutor or mentor to help you out.

Good luck and I hope you find the help you need!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020