I am working on my first OOP project using python and tkinter. The task assigned to me is to build the GUI of a supermarket that allows the user to do different things depending on the role with which he logs in, being able to start as a cashier or as an administrator. Right now I am working on the part where the administrator must be able to create a new product within 5 predefined categories. The problem that arises is when it comes to having to add an image to the product that is being created. I have tried various methods but have not been able to get it to work. My idea is that the program takes the image you select, moves it to a specific folder on the computer and renames it using an ID that the code already generates. I know that if I can solve it for just one type of product I can adapt to the other ones.
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from PIL import Image, ImageTk
from database import productos
class Nuevo_producto:
def __init__(self):
root = Tk()
self.marcalataentry=StringVar()
self.nombrelataentry=StringVar()
self.preciolataentry=IntVar()
self.mensalata=StringVar()
root.title("Nuevos productos")
self.label=ttk.Label(root, text="Seleccione la categoría del producto a añadir").pack()
self.button1=ttk.Button(root, text="Enlatados", command=self.nuevo_enlatado).pack()
self.button2=ttk.Button(root, text="Bebidas Alcohólicas", command=self.nueva_bebida_alcohol).pack()
self.button3=ttk.Button(root, text="Bebidas gaseosas", command=self.nueva_bebida_gas).pack()
self.button4=ttk.Button(root, text="Carne/pollo/pescado", command=self.nueva_carne).pack()
self.button5=ttk.Button(root, text="Lácteos", command=self.nuevo_lacteo).pack()
#
root.mainloop()
def nuevo_enlatado(self, img):
self.nuevoenlatado=Toplevel()
self.nuevoenlatado.title("Añadir un producto enlatado")
self.marcaenlatado=ttk.Label(self.nuevoenlatado, text="Ingrese marca del producto").pack()
self.marcaenlatadoentry=ttk.Entry(self.nuevoenlatado, textvariable=self.marcalataentry).pack()
self.nombreenlatado=ttk.Label(self.nuevoenlatado, text="Ingrese nombre del producto").pack()
self.nombreenlatadoentry=ttk.Entry(self.nuevoenlatado, textvariable=self.nombrelataentry).pack()
self.precioenlatado=ttk.Label(self.nuevoenlatado, text="Ingrese el precio del producto").pack()
self.precioenlatadoentry=ttk.Entry(self.nuevoenlatado, textvariable=self.preciolataentry).pack()
self.button=ttk.Button(self.nuevoenlatado, text="Seleccionar imágen", command=self.buscar_imagen).pack()
self.aviso=ttk.Label(self.nuevoenlatado, textvariable=self.mensalata)
self.foto=ttk.Label(self.nuevoenlatado, image=img).pack()
self.foto.image = img
self.añadir=ttk.Button(self.nuevoenlatado, text="Añadir", command=self.añadir_lata).pack()
self.salir=ttk.Button(self.nuevoenlatado, text="Cancelar", command=self.nuevoenlatado.destroy).pack()
def añadir_lata(self):
if productos[0]=="Enlatados":
marcatemp=self.marcalataentry.get()
nombretemp=self.nombrelataentry.get()
preciotemp=self.preciolataentry.get()
idtemp=self.generar_id(self.nuevoID)
listatemp=["Enlatados", marcatemp, idtemp, nombretemp, preciotemp, 10]
productos.append(listatemp)
print(productos)
listatemp=[]
def nueva_bebida_alcohol(self):
pass
def nueva_bebida_gas(self):
pass
def nueva_carne(self):
pass
def nuevo_lacteo(self):
pass
def buscar_imagen(self):
file = filedialog.askopenfilename(initialdir = "/", title = "Seleccione la imágen del producto a añadir",
filetypes = (("Imagen","*.jpg"),("Imagen","*.png")))
image = Image.open(fr"{file}")
resize_image = image.resize((200, 200))
img = ImageTk.PhotoImage(resize_image)
self.label1 = Label(image=img)
self.label1.image = img
#self.label1.pack()
return img
def generar_id(self):
ids=[]
for i in productos:
ids.append(i[-4])
max_value=max(ids)
self.nuevoID=max_value+1
ids=[]
return(self.nuevoID)
def main():
mi_app = Nuevo_producto()
return 0
if __name__ == '__main__':
main()