Hugo Phelan
Hugo Phelan

Reputation: 25

my tkinter window appears after I have ended the program

I am making a weather station using a raspberry pi and I am trying to use a tkinter window to display the data, however, the window only appears after I have used ctrl + c to end the program. I am using the terminal to run the code (the command is sudo python test.py)

Does anyone know what I am doing wrong?

from WeatherPiHumiture import *
from WeatherPiBarometer import *
import Tkinter as tkinter

window = tkinter.Tk()

def main():
    while True:
                result = read_dht11_dat()
                if result:
                        humidity, temperature = result
                        humid = "Humidity: %s %% " % (humidity)
                        humids = tkinter.Label(
                            text=humid,
                            fg='black',
                            bg='white',
                            height=2,
                            width=40
                        )

                #Barometer
                sensor = BMP085.BMP085()
                temp = sensor.read_temperature()    # Read temperature to veriable temp
                pressure = sensor.read_pressure()
                
                temperatures = 'Temperature: {0:0.2f} C'.format(temp)# Print temperature
                temperate = tkinter.Label(
                        text=temperatures,
                        fg='black',
                        bg='white',
                        height=2,
                        width=40
                )
            
                pressure = '{0:0.2f}'.format(pressure)
                pressure = float(pressure)
                millibar = pressure / 100
                millibar = str(millibar)
                pressured = 'Pressure: ' + millibar + ' MilliBar'
                pressures = tkinter.Label(
                        text=pressured,
                        fg='black',
                        bg='white',
                        height=2,
                        width=40
                )

                temperate.grid(row=0, column=0)
                pressures.grid(row=0, column=1)
                humids.grid(row=0, column=2)
                
                time.sleep(1)

def destroy():
    GPIO.cleanup()

if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        destroy()


window.mainloop()

Upvotes: 0

Views: 83

Answers (1)

Tresdon
Tresdon

Reputation: 1211

Add window.mainloop() to your try block

if __name__ == '__main__':
    try:
        main()
        window.mainloop()
    except KeyboardInterrupt:
        destroy()
        window.destroy()

Upvotes: 0

Related Questions