Guten Tag,
Wir haben an unserer Hochschule ein Projekt bekommen eine Fotobox mit Python zu programmieren. Der Code steht soweit. Wir wollen die Oberfläche durch einen Button erweitern in dem eine Filter Funktion funktionieren soll. Soll heißen: der Kunde soll den Button anklicken, ein weiteres Interface soll sich öffnen und er soll die Möglichkeit bekommen, zwischen verschiedenen Filtern auswählen zu können. Unser Problem: Den Button haben wir erstellt und wir kommen in ein neues Interface, jedoch können wir auf dem neuen Interface keine Buttons hinzufügen. Könnte sich jemand der sich mit Python auskennt über den Code schauen und evtl. sagen wie man dieses Problem lösen kann?
import PIL
from PIL import Image, ImageTk
from os import *
from picamera import PiCamera
from time import sleep
from datetime import *
from picamera import Color
from os import walk
import os
import datetime
import time
from glob import *
from os import listdir
import RPi.GPIO as GPIO
from tkinter import messagebox
import cups
import io
import subprocess
from tkinter import ttk
class MainGui:
def __init__(self):
self.cam = PiCamera()
now = datetime.datetime.now()
self.daytag = str(now.day) + "_" + str(now.month) + "_" + str(now.year) + "/"
self.usbPfad = "/media/pi/BD23-DD96/FotoBox_Bilder/" #falls Stick vorhanden, dieser Pfad
self.defPfad = "/home/pi/Bilder/" #falls kein Stick vorhanden, Verzeichnis auf microSD
self.mainWindow = Tk()
self.mainWindow.attributes("-fullscreen",True) #Vollbild Programm
self.mainWindow.title("Fotobox")
self.mainWindow.configure(background="#ffffff")
self.mainWindow.geometry("1280x720")
GPIO.setmode(GPIO.BCM)
GPIO.setup(19, GPIO.OUT)
GPIO.setwarnings(False)
#Read Pic counter from File
self.counter = 0
self.counter = self.readCounter()
#Header
header = Frame(self.mainWindow,borderwidth=0)
header.pack(side=TOP)
#Close Button
imgClose = ImageTk.PhotoImage(Image.open("/home/pi/Schreibtisch/FotoBox_V2/close_img.png"))
Button(self.mainWindow,borderwidth=0,background="#ffffff",height=100,width=100,highlightthickness = 0,image=imgClose,command=self.askToQuit).place(relx=0.95,anchor=N)
#Logo
img = ImageTk.PhotoImage(Image.open("/home/pi/Schreibtisch/FotoBox/0.png")) #Logo HSPF
imgLabel = Label(header,text="HOCHSCHULE PFORZHEIM", font="Sans 20 bold" ,image = img,borderwidth=0)
imgLabel.pack()
#Body
frame = Frame(self.mainWindow,borderwidth=0)
frame.configure(background="#000000")
frame.pack(expand=NO,pady=20)
#Buttons Einzelbild, Serienbild, Galerie:
Button(frame,borderwidth=2,text="Einzelbild",bg="#EBBB32",padx=8,pady=5, font="Sans 20 bold", height = 5, width = 15,command = self.einzelbild_click).pack(side=LEFT)
Button(frame,borderwidth=2,text="Serienbild",bg="#EBBB32",padx=8,pady=5, font="Sans 20 bold", height = 5, width = 15,command=self.serienbilder_click).pack(side=LEFT)
Button(frame,borderwidth=2,text="Galerie",bg="#EBBB32",padx=8,pady=5, font="Sans 20 bold", height = 5, width = 15,command = self.galerie_click).pack(side=LEFT)
#Footer
footer = Frame(self.mainWindow,borderwidth=0)
footer.configure(background="#ffffff")
footer.pack(side=BOTTOM)
self.lbl = Label(footer,bg="#ffffff",font="Sans 16 bold",fg="#F56363")
self.lbl.pack()
Button(footer,borderwidth=0,text="HOW TO",bg="#ffffff",height=50,width=20,highlightthickness = 0,font="Sans 20 bold",command=self.howTo_Click).pack(side=TOP,padx=2,pady=60)
self.mainWindow.mainloop()
#Einzelbild 1 Foto
def einzelbild_click(self):
self.take_photo(1)
#Serienbild 4 Fotos
def serienbilder_click(self):
self.take_photo(4)
#Open Gallery
def galerie_click(self):
bilderList = []
bilderList = glob(str(self.getFileDir()) + "*.jpg")
if len(bilderList) == 0:
self.lbl["text"] = "Galerie ist leer!"
self.lbl.update()
sleep(2)
self.lbl["text"] = ""
self.lbl.update()
else:
gallery = ImgWindow(bilderList,True)
def howTo_Click(self):
howto = HowToWindow()
def close_mainWindow(self):
self.mainWindow.destroy()
def take_photo(self,bilderAnzahl):
bilderList = []
self.cam.resolution = (2592,1944)
self.cam.hflip = True
self.cam.framerate = 15
self.cam.start_preview()
for i in range(bilderAnzahl):
self.cam.annotate_text_size = 150
self.cam.annotate_background = Color('black')
self.cam.annotate_foreground = Color('white')
if bilderAnzahl > 1:
self.cam.annotate_text = " Bild: " + str(i + 1) + " von " + str(bilderAnzahl) + " "
else:
self.cam.annotate_text = " Bereit ?! "
sleep(2)
#Time as Pic Name
#pfadDir = str(self.getFileDir() + "Pic_" + str(int(time.time())) + ".jpg")
#Counter as Pic Name
self.counter += 1
self.updateCounter()
pfadDir = str(self.getFileDir() + "Pic_" + str(self.counter) + ".jpg")
self.cam.annotate_text = ""
#Timer
for i in range(5):
self.cam.annotate_text_size = 150
self.cam.annotate_background = Color('black')
self.cam.annotate_foreground = Color('white')
if 5 - i == 1:
GPIO.output(19, GPIO.HIGH)
self.cam.annotate_text = " Bitte laecheln :-) "
else:
self.cam.annotate_text = str(5 - i)
sleep(1)
self.cam.annotate_text = ""
self.cam.capture(pfadDir)
bilderList.append(pfadDir)
GPIO.output(19, GPIO.LOW)
self.cam.stop_preview()
preview = ImgWindow(bilderList,False)
def getFileDir(self):
if os.path.isdir(self.usbPfad):
if not os.path.isdir(str(self.usbPfad + self.daytag)):
os.makedirs(self.usbPfad + self.daytag)
return str(self.usbPfad + self.daytag)
elif os.path.isdir(self.defPfad):
if not os.path.isdir(self.defPfad + self.daytag):
os.makedirs(self.defPfad + self.daytag)
return str(self.defPfad + self.daytag)
#Quit Yes or No
def askToQuit(self):
if messagebox.askyesno("Photobox","Wollen Sie das Programm wirklich beenden?"):
self.close_mainWindow()
def setCounter(self):
self.counter = self.readCounter()
def readCounter(self):
file = open("/home/pi/Schreibtisch/FotoBox_V2/counter.txt",os.O_RDONLY|os.O_CREAT)
count = int(read(file,100))
close(file)
return count
def updateCounter(self):
file = open("/home/pi/Schreibtisch/FotoBox_V2/counter.txt",os.O_WRONLY|os.O_CREAT)
count = str.encode(str(self.counter))
write(file,count)
close(file)
class ImgWindow:
def __init__(self,photoList,useGalleryGUI):
self.pfad = []
self.pfad = photoList
self.useGalleryGUI = useGalleryGUI
self.pointer = 0
self.imgWindow = Toplevel()
self.imgWindow.configure(background="#ffffff")
self.imgWindow.attributes("-fullscreen",True)
self.img = ImageTk.PhotoImage(Image.open(self.pfad[self.pointer]))
self.panel = Label(self.imgWindow,bg="#ffffff",height=600,image = self.img) #Rahm von Bild
self.panel.pack(padx=2,pady=10,fill=BOTH,expand=YES)
self.frame =Frame(self.imgWindow,bg="#ffffff")
self.frame.pack(side=BOTTOM,fill=BOTH)
self.homeImg = ImageTk.PhotoImage(Image.open("/home/pi/Schreibtisch/FotoBox_V2/home.png"))
self.printImg = ImageTk.PhotoImage(Image.open("/home/pi/Schreibtisch/FotoBox_V2/print.png"))
#Init Printer
startPrinter = "cupsenable Canon_SELPHY_CP1300"
subprocess.call(startPrinter, shell=True)
if self.useGalleryGUI:
Button(self.frame,text="<",bg="#EBBB32",fg="#ffffff",font="Sans 20 bold", height = 3, width = 10,command=self.showPrevPic).pack(side=LEFT)
Button(self.frame,text=">",bg="#EBBB32",fg="#ffffff",font="Sans 20 bold", height = 3, width = 10,command=self.showNextPic).pack(side=RIGHT)
self.frame2 = Frame(self.frame,height=30,width=250,bg="#ffffff")
self.frame2.pack(side=BOTTOM)
Button(self.frame2,borderwidth=0,highlightthickness=0,bg="#ffffff",height=50,width=130,padx=1,pady=1,image=self.homeImg,command=self.closeWindow).pack(side=RIGHT,ipady=30,padx=80)
#Print Button
Button(self.frame2,borderwidth=0,highlightthickness=0,bg="#ffffff",height=50,width=130,padx=1,pady=1,image=self.printImg,command=self.Print).pack(side=LEFT,ipady=30,padx=80)
else:
Button(self.frame,text="Behalten",bg="#1ca706",fg="#ffffff",font="Sans 20 bold", height = 3, width = 10,command=self.keepImg).pack(side=LEFT)
Button(self.frame,text="Löschen",bg="#993232",fg="#ffffff",font="Sans 20 bold", height = 3, width = 10,command=self.delete_image).pack(side=RIGHT)
self.frame2 = Frame(self.frame,height=30,width=50,bg="#ffffff")
self.frame2.pack(side=BOTTOM)
#Print Button
Button(self.frame2,borderwidth=0,highlightthickness=0,bg="#ffffff",height=30,width=120,padx=1,pady=1,image=self.printImg,command=self.Print).pack(side=BOTTOM,ipady=22,padx=0)
txtLabel = str(1) + " von " + str(len(self.pfad))
self.l = Label(self.frame2,bg="#ffffff",font="Sans 17 bold",fg="#EBBB32",text=txtLabel)
self.l.pack(side=TOP)
self.makePreview(0)
self.imgWindow.mainloop()
def closeWindow(self):
self.imgWindow.destroy()
def makePreview(self,pointer):
#Resize Image for Preview
self.img = ImageTk.PhotoImage(Image.open(self.pfad[pointer]).resize((1280,720),Image.ANTIALIAS))
self.panel["image"] = self.img
self.l["text"] = str(pointer + 1) + " von " + str(len(self.pfad))
self.l.update()
def showNextPic(self):
if self.pointer + 1 < len(self.pfad):
self.pointer +=1
else:
self.pointer = 0
self.makePreview(self.pointer)
def showPrevPic(self):
if self.pointer > 0:
self.pointer -= 1
else:
self.pointer = len(self.pfad) - 1
self.makePreview(self.pointer)
def keepImg(self):
if self.pointer < len(self.pfad) - 1:
self.pointer += 1
self.makePreview(self.pointer)
else:
self.closeWindow()
def delete_image(self):
if path.exists(str(self.pfad[self.pointer])):
remove(str(self.pfad[self.pointer]))
if self.pointer < len(self.pfad) - 1:
self.pointer += 1
self.makePreview(self.pointer)
else:
self.closeWindow()
#Print Function
def Print(self):
cmd = 'lp -d Canon_SELPHY_CP1300 -o raw ' + self.pfad[self.pointer]
subprocess.call(cmd, shell=True)
conn = cups.Connection()
printers = conn.getPrinters()
cups.setUser('pi')
txt = str(printers['Canon_SELPHY_CP1300']['printer-state-reasons']) #Check for Error
print(txt) #print error
if (txt == "['none']"):
waitSec = 0
progress = ttk.Progressbar(self.panel,value=waitSec,length=200,max=35, mode="determinate")
progress.pack(side=BOTTOM,pady=20)
for i in range(35):
waitSec = waitSec + 1
sleep(1)
progress['value'] = waitSec
if(i%2 == 0):
self.l["text"] ='Printing.'
else:
self.l["text"] ='Printing..'
self.l.update()
progress.update()
self.l["text"] = str(self.pointer + 1) + " von " + str(len(self.pfad))
self.l.update()
progress.destroy()
#self.ImgWindow.update()
else:
errorText = "Fehler: Drucker ist nicht verbunden!!"
error = Label(self.panel,bg="#ffffff",font="Sans 14 bold",fg="#EBBB32",text=errorText)
error.pack(side=BOTTOM)
self.panel.update()
sleep(4)
error.destroy()
for printer in printers:
print (printers)
#print (conn.printFile('Canon_SELPHY_CP1300',str(self.pfad[self.pointer]),"test",{}))
#How To window
class HowToWindow:
def __init__(self):
self.howToWindow = Toplevel()
self.howToWindow.configure(background="#ffffff")
self.howToWindow.attributes("-fullscreen",True)
#HowTo Image
self.howToimg = ImageTk.PhotoImage(Image.open("/home/pi/Schreibtisch/FotoBox_V2/howTo.jpeg"))
self.panel = Label(self.howToWindow,bg="#ffffff",height=630,image = self.howToimg)
self.panel.pack(padx=1,pady=1,fill=BOTH,expand=YES)
#Home Button
self.homeImg = ImageTk.PhotoImage(Image.open("/home/pi/Schreibtisch/FotoBox_V2/home.png"))
Button(self.howToWindow,borderwidth=0,bg="#ffffff",padx=1,pady=1,highlightthickness = 0,height=90,width=100,command=self.closeWindow,image=self.homeImg).pack(side=BOTTOM,ipady=20)
def closeWindow(self):
self.howToWindow.destroy()
#"App" erstellen
photobox = MainGui()
GPIO.cleanup()