Mar-18-2021, 10:23 AM
(This post was last modified: Mar-18-2021, 10:23 AM by deanhystad.)
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.