You want to show the current temperature reading from a sensor on a Tkinter label that updates every second. Which code snippet correctly updates the label text with the latest temperature?
import tkinter as tk import random def get_temperature(): return random.uniform(20.0, 30.0) root = tk.Tk() label = tk.Label(root, text="Temperature: -- °C") label.pack() # Which update function is correct? # Option A # def update(): # temp = get_temperature() # label.config(text="Temperature: " + str(temp) + " °C") # root.after(1000, update) # Option B # def update(): # temp = get_temperature() # label['text'] = "Temperature: " + temp + " °C" # root.after(1000, update) # Option C # def update(): # temp = get_temperature() # label.config(text=f"Temperature: {temp:.1f} °C") # root.after(1000, update) # Option D # def update(): # temp = get_temperature() # label.text = f"Temperature: {temp:.1f} °C" # root.after(1000, update) update() root.mainloop()
Remember to convert numbers to strings when concatenating, and use the correct method to update label text.
Option A uses label.config() with an f-string to format the temperature to one decimal place, which is the correct way to update the label text in Tkinter. Option A tries to concatenate a float directly to a string, causing a TypeError. Option A works but does not format the number nicely. Option A tries to assign to label.text, which does not update the label.
You have multiple sensors sending temperature and humidity data. Which data structure best organizes this data for easy access and display in a Tkinter dashboard?
Think about quick lookup by sensor ID and grouping related data together.
Option B uses a dictionary keyed by sensor ID, allowing fast access to each sensor's data. The tuple groups temperature and humidity together. Option B is workable but less efficient for lookups. Option B separates related data, making access harder. Option B is not structured data and hard to parse.
You have a table 'SensorReadings' with columns 'SensorID', 'Timestamp', and 'Temperature'. Which DAX measure calculates the average temperature per sensor correctly?
AverageTempPerSensor = CALCULATE(AVERAGE(SensorReadings[Temperature]), ALLEXCEPT(SensorReadings, SensorReadings[SensorID]))
Use ALLEXCEPT to keep the filter on SensorID while removing others.
Option C correctly calculates the average temperature per sensor by removing all filters except SensorID. Option C calculates average over all data ignoring sensor grouping. Option C removes all filters, ignoring sensor grouping. Option C calculates average but may be incorrect if there are missing values.
You created a Tkinter button to refresh sensor data, but clicking it does nothing. What is the most likely cause?
import tkinter as tk def refresh(): print("Refreshing data") root = tk.Tk() button = tk.Button(root, text="Refresh", command=refresh()) button.pack() root.mainloop()
Check how the command parameter is assigned in Tkinter buttons.
Option A is correct because command=refresh() calls the function immediately and assigns its result, instead of passing the function itself. The correct syntax is command=refresh. Option A is unrelated to functionality. Option A is incorrect; return value is not used. Option A is wrong; widgets must be packed before mainloop.
You must design a Tkinter dashboard that shows live readings from 10 sensors. Each sensor has temperature and humidity labels that update every second. What is the best approach to ensure the GUI remains responsive and updates correctly?
Tkinter is not thread-safe; consider how to update UI without freezing it.
Option D is best because using root.after() schedules periodic updates in the main thread without freezing the GUI. Option D risks thread-safety issues and crashes. Option D blocks the main thread, freezing the GUI. Option D does not provide live updates.
