Jbro1945
Jbro1945

Reputation: 1

Where do I assign a variable in order for it to be used in a function (inside a class)?

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'

Upvotes: 0

Views: 46

Answers (2)

mr_pool_404
mr_pool_404

Reputation: 498

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.

Upvotes: 1

Ishita Singh
Ishita Singh

Reputation: 337

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)

Upvotes: 0

Related Questions