I have data coming in through the GPIO serial from an arduino nano. I can already use minicom to monitor that data. Now I would like to use this data to toggle between 2 images. As being relativly new to all of this I already serached around a lot and found this thread:
How do I display an image file (PNG) in a simple window?
I tried using the code supllied by HeatfanJohn but am running into some problems.
This is the code i have so far:
#!/usr/bin/python
# use a Tkinter label as a panel/frame with a background image
# note that Tkinter only reads gif and ppm images
# use the Python Image Library (PIL) for other image formats
# free from [url]http://www.pythonware.com/products/pil/index.htm[/url]
# give Tkinter a namespace to avoid conflicts with PIL
# (they both have a class named Image)
import Tkinter as tk
import PIL
from PIL import Image, ImageTk
from ttk import Frame, Button, Style
import time
class Example():
def __init__(self):
self.root = tk.Tk()
self.root.title('Display')
# pick an image file you have .bmp .jpg .gif. .png
# load the file and covert it to a Tkinter image object
imageFile = "pic1.png"
self.image1 = ImageTk.PhotoImage(Image.open(imageFile))
self.image2 = ImageTk.PhotoImage(Image.open("pic2.png"))
# get the image size
w = self.image1.width()
h = self.image1.height()
# position coordinates of root 'upper left corner'
x = 0
y = 0
# make the root window the size of the image
self.root.geometry("%dx%d+%d+%d" % (w, h, x, y))
# root has no image argument, so use a label as a panel
self.panel1 = tk.Label(self.root, image=self.image1)
self.display = self.image1
self.panel1.pack(side=tk.TOP, fill=tk.BOTH, expand=tk.YES)
print "Display image1"
self.root.after(30000, self.update_image)
self.root.mainloop()
def update_image(self):
if self.display == self.image1:
self.panel1.configure(image=self.image2)
print "Display image2"
self.display = self.image2
else:
self.panel1.configure(image=self.image1)
print "Display image1"
self.display = self.image1
self.root.after(30000, self.update_image)
def main():
app = Example()
if __name__ == '__main__':
main()
Now I get the following errors:
Traceback (most recent call last):
File "try3.py", line 16, in <module>
class Example():
File "try3.py", line 61, in Example
main()
File "try3.py", line 58, in main
app = Example()
NameError: global name 'Example' is not defined