Holooly Plus Logo

Question 12.11: You have a sensor wired to your Raspberry Pi, but want a big......

You have a sensor wired to your Raspberry Pi, but want a big digital display of the reading on the screen.

Step-by-Step
The 'Blue Check Mark' means that this solution was answered by an expert.
Learn more on how do we answer questions.

Use the Tkinter library to open a window and write the reading on it as a label with a large font (Figure 12-19).

This example uses data from the ultrasonic rangefinder of Recipe 12.10. So, complete that recipe first if you want to try out this example.

To test out the rangefinder, open an editor (nano or idle) and paste in the following code. As with all the program examples in this book, you can also download the program from the code section of the book’s website, where it is called gui_sensor_reading.py. The code for measuring distances using the ultrasonic sensor is the same as Recipe 12.10, so only the section of the code concerned with displaying the value is repeated here:

class App:
def __init__(self, master):
self.master = master
frame = Frame(master)
frame.pack()
label = Label(frame, text=’Distance (inches)’, font=(“Helvetica”, 32))
label.grid(row=0)
self.reading_label = Label(frame, text=’12.34′, font=(“Helvetica”, 110))
self.reading_label.grid(row=1)
self.update_reading()
def update_reading(self):
cm, inch = get_distance()
reading_str = “{:.2f}”.format(inch)
self.reading_label.configure(text=reading_str)
self.master.after(500, self.update_reading)
root = Tk()

root.wm_title(‘Range Finder’)
app = App(root)
root.geometry(“400×300+0+0”)
root.mainloop()

Discussion

Athough this recipe uses a distance sensor, it would work equally well with the other sensor recipes in this chapter. You just need to change the labels and the method of obtaining a reading from the sensor.

See Also

For information on formatting numbers to a certain number of decimal places, see Recipe 7.1.

12.9

Related Answered Questions

Question: 12.10

Verified Answer:

Use a low cost SR-04 rangefinder. These devices ne...
Question: 12.12

Verified Answer:

Write a Python program that writes the data to a f...
Question: 12.9

Verified Answer:

Use the DS18B20 digital temperature sensor. This d...
Question: 12.7

Verified Answer:

Use an MCP3008 ADC chip. However, unless you need ...
Question: 12.8

Verified Answer:

Use an analog accelerometer with a MCP3008 ADC chi...
Question: 12.6

Verified Answer:

Use a potential divider with one fixed resistor an...
Question: 12.2

Verified Answer:

Use the same basic recipe and code as Recipe 12.1,...
Question: 12.3

Verified Answer:

Low-cost resistive gas sensors are available that ...
Question: 12.4

Verified Answer:

The Raspberry Pi GPIO connector has only digital i...
Question: 12.5

Verified Answer:

Use a pair of resistors to act as a voltage divide...