0

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
2

The code does not work because you have altered its meaning.

In Python white space is syntactically important. You must preserve white space. In particular the indent levels of code determine code blocks.

update_image is meant to be a member function of class Example. The code, as you have presented it, will not compile.

3
  • Thank you ver much for replying. I edited the code to reflect your changes. Unfortunatly I get a new set of error messages I also updated those. Could you help me once again? Thank you very much!!!
    – zwafro
    Dec 16 '15 at 15:42
  • The main parts are not part of the Example class. You need to deindent the def main and if __name__ parts.
    – joan
    Dec 16 '15 at 15:55
  • You are officially my best friend now :) It worked, thank you so much!!!
    – zwafro
    Dec 16 '15 at 15:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.