Python Forum
[Tkinter] Creating a dashboard for a Pi that switches feeds.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Creating a dashboard for a Pi that switches feeds.
#1
I'm having a hard time getting this to work. I have a Raspberry Pi with a 4 inch screen that I am trying to make a display that shows the time and then when you click it switches to the cpu temperature. Right now it displays right but when I click it goes into a loop of the two functions I made. Any help would be great!

from tkinter import *
from tkinter import ttk
from tkinter import font
from gpiozero import CPUTemperature
import time

cpu = CPUTemperature()
temp = f'{cpu.temperature}'


def quit(*args):
    root.destroy()

def mouseClicked(event):
    txt.set(time.strftime("%H :%M :%S"))
    root.after(1000,show_time)
    
    
def show_time():
    txt.set(time.strftime("%H :%M :%S"))
    root.after(1000,show_time)

def show_temp():
    txt.set(f'{cpu.temperature}' + ' C')
    root.after(1000,show_temp)
    
root = Tk()
root.attributes("-fullscreen",True)
root.configure(background='black')
root.bind("<Escape>",quit)
root.bind("<Button-1>",mouseClicked)
root.after(1000,show_temp)

fnt = font.Font(family='Helvetica', size=128, weight='bold')
txt = StringVar()
txt.set(f'{cpu.temperature}' + ' C')
lbl = ttk.Label(root, textvariable=txt, font=fnt, foreground="green", background="black")
lbl.place(relx=0.5, rely=0.5, anchor=CENTER)

root.mainloop()
Larz60+ write Mar-17-2021, 06:25 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use bbcode tags on future posts.
Reply
#2
You should have a function that gets called every second. The function should display the CPU temp or the time based on if the mouse button is pressed. Something like this except it displays temperature instead of "Hello World"
import time
import tkinter as tk

font = ('Helvetica', 128, 'bold')
button_pressed = False

def button_event(event):
    """Call me when mouse button 1 is pressed or relased"""
    global button_pressed
    button_pressed = event.type is tk.EventType.ButtonPress
    update_display()

def update_display():
    """Display time or temperature based on if mouse button 1 is pressed"""
    if button_pressed:
        display['text'] = 'Hello world'
    else:
        display['text'] = time.strftime("%H :%M :%S")

def update_loop():
    """Update display once a second"""
    update_display()
    root.after(1000, update_loop)

root = tk.Tk()
root.configure(bg='black')
root.bind("<Escape>", root.destroy)
root.bind("<Button-1>" , button_event)
root.bind("<ButtonRelease-1>" , button_event)
display = tk.Label(root, font=font, fg="green", bg="black")
display.pack()
update_loop()   # Starts the display update
root.mainloop()
Since the update loop continues to run, this would update the CPU temp every second as long as the mouse button is pressed.
mrbojangles1814 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Flying a Tello Drone via Node-Red Dashboard G_rizzle 1 1,808 Feb-18-2021, 11:38 AM
Last Post: Larz60+
  Free Django Dashboard Template jett21 2 2,046 Mar-03-2020, 05:21 AM
Last Post: jett21
  [Tkinter] Car dashboard with TKinter TMJJ 1 7,751 Dec-28-2016, 05:56 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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