1
\$\begingroup\$

I am building a simple Store Management System and I have 4 classes, 3 of which are for Product, Vendor and Orders. The last one is my main class where the actual GUI is built. On it I have 800 lines of different functions that represent the different views and actions in the program.

Is that ok? Should I make a class for each view or should I leave it like this?

Small part of the code:

def productView(self):
    self.productDisplay.pack()
    self.productDisplay1 = tk.Frame(self.productDisplay)
    self.productDisplay2 = tk.Frame(self.productDisplay)
    self.productDisplay1.pack(side='top')
    self.productDisplay2.pack(side='top')
    self.productDisplay3 = tk.Frame(self.productDisplay2,bd = 1,relief='sunken')
    self.productDisplay4 = tk.Frame(self.productDisplay2,bd = 1,relief='sunken')
    self.productDisplay5 = tk.Frame(self.productDisplay2,bd = 1,relief='sunken')
    self.productDisplay3.pack(side='left',anchor='n')
    self.productDisplay4.pack(side='left',anchor='n')
    self.productDisplay5.pack(side='left',anchor='n')
    tk.Button(self.productDisplay1, text='Add Product', command = lambda : self.packer(self.productDisplay, self.addProduct) ).grid(row=0,column = 0)
    tk.Button(self.productDisplay1, text='Edit Product', command = lambda: self.packer(self.productDisplay, self.editProduct) ).grid(row=0,column = 1)
    tk.Button(self.productDisplay1, text='Delete Product', command = lambda : self.packer(self.productDisplay,self.deleteProduct)  ).grid(row=0,column = 2)
    tk.Button(self.productDisplay1, text='View Product', command = lambda: self.packer(self.productDisplay,self.viewProduct) ).grid(row=0,column = 3)
    tk.Button(self.productDisplay1, text='Back', command = lambda : self.packer(self.productDisplay,self.main) ).grid(row=0,column = 4)

    tk.Label(self.productDisplay3,text = 'Added Products',relief='raised',width=25).grid(row=0,columnspan=2)
    tk.Label(self.productDisplay3,text = 'ID',width=5).grid(row=1,column=0)
    tk.Label(self.productDisplay3,text = 'Name').grid(row=1,column=1)
    added_row=2
    for product in self.recentProductsAdd:
        tk.Label(self.productDisplay3,text = product[0]).grid(row=added_row,column=0)
        tk.Label(self.productDisplay3,text = product[1]).grid(row=added_row,column=1)
        added_row+=1

    tk.Label(self.productDisplay4,text = 'Edited Products',relief='raised',width=25).grid(row=0,columnspan=2)
    tk.Label(self.productDisplay4,text = 'ID',width=5).grid(row=1,column=0)
    tk.Label(self.productDisplay4,text = 'Name').grid(row=1,column=1)
    edited_row = 2
    for product in self.recentProductsEdit:
        tk.Label(self.productDisplay4,text = product[0]).grid(row=edited_row,column=0)
        tk.Label(self.productDisplay4,text = product[1]).grid(row=edited_row,column=1)
        edited_row+=1

    tk.Label(self.productDisplay5,text = 'Deleted Products',relief='raised',width=25).grid(row=0,columnspan=2)
    tk.Label(self.productDisplay5,text = 'ID',width=5).grid(row=1,column=0)
    tk.Label(self.productDisplay5,text = 'Name').grid(row=1,column=1)
    deleted_row = 2
    for product in self.recentProductsDelete:
        tk.Label(self.productDisplay5,text = product[0]).grid(row=deleted_row,column=0)
        tk.Label(self.productDisplay5,text = product[1]).grid(row=deleted_row,column=1)
        deleted_row+=1

def addProduct(self):
    self.addProductDisplay.pack()
    tk.Button(self.addProductDisplay, text='Save', command = self.saveProduct ).grid(row=len(self.productOptions),column = 1)
    tk.Button(self.addProductDisplay, text='Cancel', command = lambda : self.packer(self.addProductDisplay,self.productView) ).grid(row=len(self.productOptions),column = 0)
    for i in xrange(len(self.productOptions)):
        tk.Label(self.addProductDisplay, text = self.productOptions[i]).grid(row=i, column=0)
        tk.Entry(self.addProductDisplay, textvariable = self.productVars[self.productOptions[i].lower()]).grid(row=i,column = 1)

def saveProduct(self):
    try:
        p = Product()
        p.name = self.productVars['name'].get()
        p.idNumber = self.productVars['id'].get()
        p.price = self.productVars['price'].get()
        p.quantity = self.productVars['quantity'].get()
        p.unit = self.productVars['unit'].get()
        p.case = self.productVars['case'].get()
        self.clearVars(1)
        if p.idNumber in self.products.keys():
            mb.showwarning('ID taken','There is a product with that ID')

        else:
            self.products[p.idNumber] = p
            self.checkRecent(self.recentProducts,self.recentProductsAdd,(p.idNumber,p.name),'A')
            self.logger.debug('Product created : ID - %s, Name - %s, Price - %s, Unit - %s, Case - %s '%(p.idNumber,p.name,p.price,p.unit,p.case))
            self.packer(self.addProductDisplay,self.productView)
    except ValueError: 
        mb.showwarning('Error','Price,ID,Quantity and Units per Case have to be numbers')

def editSave(self,product):
    product.name = self.productVars['name'].get()
    product.quantity = self.productVars['quantity'].get()
    product.price = self.productVars['price'].get()
    product.unit = self.productVars['unit'].get()
    product.case = self.productVars['case'].get()
    self.logger.debug('Product Edited : ID - %s, Name - %s, Price - %s, Unit - %s, Case - %s '%(product.idNumber,product.name,product.price,product.unit,product.case))
    self.clearVars(1)
    self.checkRecent(self.recentProducts,self.recentProductsEdit,(product.idNumber,product.name), 'E')
    self.packer(self.editProductDisplay, self.productView)


def editProduct(self):
    self.editProductDisplay1 = tk.Frame(self.editProductDisplay)
    self.editProductDisplay2 = tk.Frame(self.editProductDisplay)
    self.editProductDisplay.pack()
    self.editProductDisplay1.pack()
    tk.Entry(self.editProductDisplay1,textvariable = self.queryProductID).grid(row=0,columnspan=2)
    tk.Button(self.editProductDisplay1, text='Search', command = self.searchID ).grid(row=1,column = 1)
    tk.Button(self.editProductDisplay1, text='Cancel', command = lambda : self.packer(self.editProductDisplay,self.productView,option = 1) ).grid(row=1,column = 0)

def searchID(self):
    try:
        product = self.products[self.queryProductID.get()]
        self.editProductDisplay2.pack()
        self.productVars['name'].set(product.name)
        self.productVars['price'].set(product.price)
        self.productVars['quantity'].set(product.quantity)
        self.productVars['unit'].set(product.unit)
        self.productVars['case'].set(product.case)
        tk.Label(self.editProductDisplay2, text = 'Name').grid(row=1,column = 0)
        tk.Entry(self.editProductDisplay2, textvariable = self.productVars['name']).grid(row=1,column=1)
        tk.Label(self.editProductDisplay2, text = 'Price').grid(row=2,column = 0)
        tk.Entry(self.editProductDisplay2, textvariable = self.productVars['price']).grid(row=2,column=1)
        tk.Label(self.editProductDisplay2, text = 'Quantity').grid(row=3,column = 0)
        tk.Entry(self.editProductDisplay2, textvariable = self.productVars['quantity']).grid(row=3,column=1)
        tk.Label(self.editProductDisplay2, text = 'Unit').grid(row=4,column = 0)
        tk.Entry(self.editProductDisplay2, textvariable = self.productVars['unit']).grid(row=4,column=1)
        tk.Label(self.editProductDisplay2, text = 'Case').grid(row=5,column = 0)
        tk.Entry(self.editProductDisplay2, textvariable = self.productVars['case']).grid(row=5,column=1)
        tk.Button(self.editProductDisplay2, text='Save', command = lambda: self.editSave(product)).grid(row=6,columnspan=2)
    except KeyError:
        mb.showwarning('Not Found', 'No such product in the database')
\$\endgroup\$

closed as off-topic by Sᴀᴍ Onᴇᴌᴀ, chux, t3chb0t, Mast, Quill Nov 3 '18 at 19:56

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Sᴀᴍ Onᴇᴌᴀ, t3chb0t, Mast, Quill
If this question can be reworded to fit the rules in the help center, please edit the question.

2
\$\begingroup\$

First off, to answer your question, I think making classes for each view would be much better in this situation. The current way you're doing this with all these functions is cumbersome.

Secondly, you have some style issues regarding PEP8, the official style guide for Python. Here's a list of some of those issues.

  1. Function and variable names should be in snake_case, not camelCase, and classes should be in PascalCase.
  2. There should be two blank lines between each top-level function/class/code block.
  3. You have whitespace inconsistencies. There should always be space between operators, except in special conditions. See PEP8 for the guidelines regarding this.

Anyways, other than that, your code is looking pretty good!

\$\endgroup\$

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