0

I'm creating a class using tkinter that lets you input multiple product's information, and I've got everything else down except for changing the entry fields to set values for the other products.

I'm putting the product changeover process into a function called saveVars which saves the entered information to the specific product variable, and then clears the entry fields, and switches the saveVars to be performed on the second product variable.

    i = 1

    def saveVars(i):
        if i == 1:
            product1.productName = self.prodName.get()
            product1.productID = self.prodID.get()
            product1.productSize = self.prodSize.get()
            product1.productPrice = self.prodPrice.get()
            product1.productQuant = self.quantity.get()
        elif i == 2:
            product2.productName = self.prodName.get()
            product2.productName = self.prodID.get()
            product2.productSize = self.prodSize.get()
            product2.productPrice = self.prodPrice.get()
            product2.productQuant = self.quantity.get()
        elif i == 3:
            product3.productName = self.prodName.get()
            product3.productName = self.prodID.get()
            product3.productSize = self.prodSize.get()
            product3.productPrice = self.prodPrice.get()
            product3.productQuant = self.quantity.get()
        newProduct()
        i += 1
        return i

I'm expecting to get it to switch the variable the entries are being saved to to the next respective product based on a +1 function, I'm having it return the i function as the new i, which should then save the entries to the next variable in the process, but it keeps telling me that I'm 'missing 1 required positional argument: 'i'

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Jbro1945
  • 1
  • 3

2 Answers2

1

You are trying to call the function without parameter as the command attribute does not take parameters just the name of the function.

To pass parameters you could use partial form functools package

import statement :

from functools import partial

your call to function would look like :

addItemButton = Button(window, text = "Add to Cart", fg='black',bg='yellow',width = 10, height = 2, command = partial(saveVars,i)) addItemButton.place(x=800,y=375)

You can set the inital value to i in your class using a global variable instead of declaring it outside the class. You can save it anywhere you wish and pass it while calling the funtion.

mr_pool_404
  • 488
  • 5
  • 16
0

It seems like you are calling saveVars without the parameter in some case. To prevent this you can set a default value of i, eg.

def saveVars(i=0)
Ishita Singh
  • 297
  • 1
  • 6
  • So I tried this, but unfortunately it only keeps the saving to the first product because it keeps defaulting to 1 or whatever number is predetermined, that's why I thought it would be a good idea to establish the i variable outside the function, and then +1 inside the function and return it so it will go up by one every time. @IshitaSingh – Jbro1945 Apr 23 '19 at 07:49
  • Yes, continue with your logic of incrementing, but add this as the default value if no 'i' is passed: ``` def saveVars(i=1) ``` Run your previous code with just this change. The error should go away. – Ishita Singh Apr 26 '19 at 12:04