Bird
0
0
Raspberry Piprogramming~5 mins

Tkinter GUI for sensor dashboard in Raspberry Pi

Choose your learning style9 modes available
Introduction

A sensor dashboard shows live data from sensors in a simple window. Tkinter helps make this window on Raspberry Pi easily.

You want to see temperature and humidity from sensors on a small screen.
You need a quick visual update of sensor readings without complex software.
You want to build a simple control panel for your Raspberry Pi project.
You want to learn how to show live data in a graphical window.
You want to test sensor data visually before sending it to a cloud or database.
Syntax
Raspberry Pi
import tkinter as tk

root = tk.Tk()
root.title('Sensor Dashboard')

label = tk.Label(root, text='Sensor Value:')
label.pack()

root.mainloop()

tk.Tk() creates the main window.

Label shows text or data on the window.

Examples
This shows a label with a fixed temperature value.
Raspberry Pi
label = tk.Label(root, text='Temperature: 25°C')
label.pack()
This updates the label every second with new sensor data.
Raspberry Pi
def update_label():
    label.config(text=f'Temperature: {sensor_value}°C')
    root.after(1000, update_label)

update_label()
This sets the window size to 300 pixels wide and 200 pixels tall.
Raspberry Pi
root.geometry('300x200')
Sample Program

This program creates a window showing temperature and humidity. It updates the values every 2 seconds with simulated data.

Raspberry Pi
import tkinter as tk
import random

root = tk.Tk()
root.title('Sensor Dashboard')
root.geometry('300x150')

label_temp = tk.Label(root, text='Temperature: -- °C', font=('Arial', 16))
label_temp.pack(pady=10)

label_hum = tk.Label(root, text='Humidity: -- %', font=('Arial', 16))
label_hum.pack(pady=10)

def update_sensor_data():
    temp = round(random.uniform(20, 30), 1)  # Simulated temp
    hum = round(random.uniform(40, 60), 1)   # Simulated humidity
    label_temp.config(text=f'Temperature: {temp} °C')
    label_hum.config(text=f'Humidity: {hum} %')
    root.after(2000, update_sensor_data)  # Update every 2 seconds

update_sensor_data()
root.mainloop()
OutputSuccess
Important Notes

Use root.after(milliseconds, function) to update data regularly.

Keep the GUI responsive by avoiding long blocking code inside update functions.

Use clear fonts and spacing for easy reading on small screens.

Summary

Tkinter lets you build simple windows to show sensor data on Raspberry Pi.

Use labels to display values and update them regularly with after.

This helps you see live sensor info without complex tools.