user12315602
user12315602

Reputation:

Is there a way to make multiple instances of a class in python

I am working on a GUI app for a bike rental service, and I need that every time when RENT A BIKE is click a new instance of the class customer is created. below is an example of the GUI enter image description here

import main
from BikeGUI.ADDSTOCK.add_stock import shp

#class which I want many instances of
customer = main.Customer()
#gui class
class Rent_Bike:
    def __init__(self, top=None):
           '''This class configures and populates the toplevel window.
           top is the toplevel containing window.''' 

#some gui definitions
        top.geometry()
        top.title()
        ...
        ...
        ...
        ...
#some more gui definitions
        self.Label1 = tk.Label(top)
        self.Label1.configure()

#more gui definitions
        self.price = tk.Entry(top)
        self.price.configure(text='''CodeName:''', .....)
        ...
        ...
        ...

#define tkinter variable
        self.num_of_bikes = tk.StringVar()
        self.Entry1.configure(insertbackground="black", textvariable=self.num_of_bikes)
        ...
        ...
        ...


    def confirm_order(self):
        #get number of bikes from entry
        customer.bikes = self.num_of_bikes.get()
        customer.rentalBasis = rent_bike_support.selected.get()
        customer.codename = self.customername.get()



if __name__ == '__main__':
    vp_start_gui()

that's the class in which I want the customer object to be instantiated several times, but the problem is that it only gets instantiated once, and it overwrites all the other customers that I created before

P.S I later need to import this customer object into another GUI file here's how I am importing it

from BikeGUI.rentbike.rent_bike import customer

but I think the problem is not with import it to another. Link to my files

Upvotes: 0

Views: 2008

Answers (2)

JacksonPro
JacksonPro

Reputation: 3275

If you want to create a new instance of customers class each time when the 'Rent A Bike' button is clicked. Use the 'command' parameter in the Button widget and set it to the class name.

For eg: if your class name is foo then it should be something like this: Button(root, text='Rent A Bike', command=foo). If you want to append each new instance to a list then use lambda function something like this command=lambda :lst.append(foo()).

Here is an example of instantiating a class within the same file:

from tkinter import *
import new_2

class Customer:
    def __init__(self, root):
        self.root = root
        self.label = Label(self.root, text='Customer')
        self.label.pack()

class foo:
     def __init__(self):
        print('Hello')
        
def print_lst():
    print(lst)

root = Tk()

lst = []

btn = Button(root, text='Rent A Bike', command=lambda :lst.append(Customer(root)) or print_lst())
#btn = Button(root, text='Rent A Bike', command=lambda :lst.append(foo()) or print_lst())
btn.pack()
root.mainloop()

To create an object for a class in another file, just import that class and follow the same procedure as above

Upvotes: 0

user9706
user9706

Reputation:

If your class is called Rent_Bike then you instantiate it with instance = Rent_Bike(). You can do this as many times as you want. An instance of a class is called an object.

Upvotes: 1

Related Questions