Home > Blockchain >  is there any solution for this selling manger issue
is there any solution for this selling manger issue

Time:08-16

I am working on a basic selling manager

from tkinter import *
from PIL import Image,ImageTk
from tkinter import filedialog

screen = Tk()
screen.title("Buyer")
screen.geometry("800x600")
w = Frame(screen)
w.grid(padx=1, pady=1)
w.pack(pady=5)

def Add_image():
    add_image.destroy()
    image =  filedialog.askopenfilename(initialdir="C:/Users/yasse/OneDrive/Documents",
               filetypes=(("all","*.*"),("PNG","*.png"),("JPEG", "*.jpeg"),("GIF", "*.gif")))
    Image.open(image).resize(size=(50,40))
    photo = ImageTk.PhotoImage(image)
    lbl = Label(w, image=photo).grid(column=0)

add_image = Button(w, text=" ", font=("helvetica", 30), command= Add_image)
add_name = Entry(w, font=("helvetica", 13))
add_price = Entry(w, font=("helvetica", 13))
add_quantity = Entry(w, font=("helvetica", 16), width=5)

class product:
    def __init__(self, image, name, price, quantity):
        self.image = image
        self.name = name
        self.price = price
        self.quantity = quantity
    def product_row(self, row):
        self.image.grid(column=0, row=row, padx=30, pady=15)
        self.name.grid(column=1, row=row, padx=30, pady=15)
        self.price.grid(column=2, row=row,padx=30, pady=15)
        self.quantity.grid(column=3, row=row, padx=30, pady=15)
def selling():
    buying_button.destroy()
    selling_button.destroy()
    screen.title("selling mode")
    product1 = product(add_image, add_name, add_price, add_quantity)
    product1.product_row(row=0)
    product2 = product(add_image, add_name, add_price, add_quantity)
    product2.product_row(row=1)
    product3 = product(add_image, add_name, add_price, add_quantity)
    product3.product_row(row=2)

buying_button = Button(w, text="buyer mode",font=("helvetica", 16), width=16, height=2)
selling_button = Button(w, text="seller mode",font=("helvetica", 16), width=16, height=2, 
                                                                        command= selling)
selling_button.pack()
buying_button.pack()

screen.mainloop()

so this code should create a multiple rows of products but for some reason only one product appears and I want also to replace the add_image button with the image but the image doesn't appears also

CodePudding user response:

This code should fix both your bugs:

from tkinter import *
from PIL import Image,ImageTk,ImageShow
from tkinter import filedialog

screen = Tk()
screen.title("Buyer")
screen.geometry("800x600")
w = Frame(screen)
w.grid(padx=1, pady=1)
w.pack(pady=5)

def Add_image(sender, row):
    image =  filedialog.askopenfilename(initialdir="C:/Users/yasse/OneDrive/Documents",
            filetypes=(("all","*.*"),("PNG","*.png"),("JPEG", "*.jpeg"),("GIF", "*.gif")))
    img = Image.open(image).resize(size=(50,40))
    photo = ImageTk.PhotoImage(img)
    btn = Button(w, image=photo)
    btn.grid(column=0, row=row)
    sender.destroy()

class product:
    def __init__(self, image, name, price, quantity):
        self.image = image
        self.name = name
        self.price = price
        self.quantity = quantity
    def product_row(self, row):
        self.image.configure(command=lambda: Add_image(self.image, row)) 
        self.image.grid(column=0, row=row, padx=30, pady=15)
        self.name.grid(column=1, row=row, padx=30, pady=15)
        self.price.grid(column=2, row=row,padx=30, pady=15)
        self.quantity.grid(column=3, row=row, padx=30, pady=15)

def create_product():
    add_image = Button(w, text=" ", font=("helvetica", 30))
    add_name = Entry(w, font=("helvetica", 13))
    add_price = Entry(w, font=("helvetica", 13))
    add_quantity = Entry(w, font=("helvetica", 16), width=5)
    return product(add_image, add_name, add_price, add_quantity)

def selling():
    buying_button.destroy()
    selling_button.destroy()
    screen.title("selling mode")
    product1 = create_product()
    product1.product_row(row=0)
    product2 = create_product()
    product2.product_row(row=1)
    product3 = create_product()
    product3.product_row(row=2)

buying_button = Button(w, text="buyer mode",font=("helvetica", 16), width=16, height=2)
selling_button = Button(w, text="seller mode",font=("helvetica", 16), width=16, height=2, 
                                                                        command=selling)
selling_button.pack()
buying_button.pack()

screen.mainloop()

The problem with the grid was due to the fact that, by giving all the products the same instances of buttons and entries, the rows where overlapped, because when you changed the row number of say a button, you were changing the row of ALL buttons. Giving each product a different instance of the inputs, I fixed this problem.

In case you can't understand any edit I made, pls let me know

  • Related