Rasberry pi / python的新手并试图修改一些代码:
我有以下内容,并希望使用外部触发器更改为下一张图像。
#!/usr/bin/env python
from Tkinter import *
import Image, ImageTk
import os, sys
import RPi.GPIO as GPIO
class Viewer:
def __init__(self, master, filelist):
GPIO.setmode(GPIO.bin)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD.UP)
GPIO.add_event_detect(23, GPIO.FALLING)
self.top = master
self.files = filelist
self.index = 0
#display first image
filename = filelist[0]
if not os.path.exists(filename):
print "Unable to find %s" % filename
self.top.quit()
self.title = Label(text=os.path.basename(filename))
self.title.pack()
im = Image.open(filename)
if im.format == "SPIDER":
im = im.convert2byte()
self.size = im.size
self.tkimage = ImageTk.PhotoImage(im, palette=256)
self.lbl = Label(master, image=self.tkimage)
self.lbl.pack(side='top')
# the button frame
fr = Frame(master)
fr.pack(side='top', expand=1, fill='x')
back = Button(fr, text="back", command=lambda : self.nextframe(-1))
back.grid(row=0, column=0, sticky="w", padx=4, pady=4)
ilabel = Label(fr, text="image number:")
ilabel.grid(row=0, column=1, sticky="e", pady=4)
self.evar = IntVar()
self.evar.set(1)
entry = Entry(fr, textvariable=self.evar)
entry.grid(row=0, column=2, sticky="w", pady=4)
entry.bind('<Return>', self.getimgnum)
next = Button(fr, text="next", command=lambda : self.nextframe(1))
next.grid(row=0, column=3, sticky="e", padx=4, pady=4)
# image doesn't appear unless put Image.open in separate function?
# and need to use tkimage.paste, not ImageTk.PhotoImage
def getImage(self, filename):
im = Image.open(filename)
if im.format == "SPIDER":
im = im.convert2byte()
if im.size != self.size:
print "all images must be same dimensions:"
f1 = os.path.basename(self.files[0])
f2 = os.path.basename(filename)
print "%s: %s, %s : %s" % (f1, str(self.size),f2, str(im.size))
self.top.quit()
return im
def nextframe(self,i=1, imgnum=-1):
if imgnum == -1:
self.index += i
else:
self.index = imgnum - 1
if self.index >= len(self.files):
self.index = 0
elif self.index < 0:
self.index = len(self.files) - 1
filename = self.files[self.index]
if not os.path.exists(filename):
print "Unable to find %s" % filename
self.top.quit()
self.title.configure(text=os.path.basename(filename))
self.evar.set(self.index+1)
im = self.getImage(filename)
self.tkimage.paste(im)
def getimgnum(self, event=None):
self.nextframe(imgnum=self.evar.get())
if __name__ == "__main__":
if not sys.argv[1:]:
print "Usage: viewseries.py images*"
sys.exit()
filelist = sys.argv[1:]
root = Tk()
app = Viewer(root, filelist)
root.mainloop()
import Tkinter
import Image
import ImageTk
import Rpi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
trig =1
def ChangeImage():
global tkpi
global trig
trig = trig + 1
image = Image.open("home/pi/Desktop/" + str(trig) + ".gif")
tkpi = ImageTk.PhotoImage(image)
label_image = Tkinter.Label (root, image=tkpi)
root.bind(GPIO.wait_for_edge(23, GPIO.FALLING), changeImage)
root.attributes("-fullscreen", True)
#root.after(500, changeImage)
tkpi = None
root = Tkinter.Tk()
changeImage()
root.mainloop()
答案 0 :(得分:0)
我明白了。
import Tkinter
import Image
import ImageTk
import Rpi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
trig =1
def ChangeImage(self):
global tkpi
global trig
trig = trig + 1
image = Image.open("home/pi/Desktop/" + str(trig) + ".gif")
tkpi = ImageTk.PhotoImage(image)
label_image = Tkinter.Label (root, image=tkpi)
root.bind(GPIO.add_event_detect(23, GPIO.FALLING callback=changeImage)
root.attributes("-fullscreen", True)
#root.after(500, changeImage)
tkpi = None
root = Tkinter.Tk()
changeImage(None)
root.mainloop()
答案 1 :(得分:0)
我对学校的RPi知识有所了解,当我们使用GertBoard上的按钮(带LED按钮的RB扩展板等)时,我们使用了添加事件检测
import Tkinter
import Image
import ImageTk
import Rpi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(23, GPIO.FALLING)
trig = 0 #are you sure you dont want this 0
#first since you increment it before adressing the image number
#keep it in mind
#unless picture numbering starts with 2
def ChangeImage():
global tkpi
global trig
trig = trig + 1 #if you set the initial value to one below this line it will be 2 for the first time. Keep it in mind
image = Image.open("home/pi/Desktop/" + str(trig) + ".gif")
tkpi = ImageTk.PhotoImage(image)
label_image = Tkinter.Label (root, image=tkpi)
root.attributes("-fullscreen", True)
GPIO.add_event_callback(23, ChangeImage)
tkpi = None #I'm quite sure this is not needed since you overwrite it when needed
root = Tkinter.Tk()
changeImage()
root.mainloop()
我们使用此处的信息: http://sourceforge.net/p/raspberry-gpio-python/wiki/Inputs/