0

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

3
  • 2
    Making multiple instances is exactly what classes are for. Commented Dec 28, 2020 at 5:11
  • how can this be achieved is we don't know how many instances we went, most information I have found is of using a for loop, but of course only when we know the number of objects we want. Commented Dec 28, 2020 at 5:31
  • Store them in a list. Commented Dec 28, 2020 at 5:51

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

the class of which I want multiple instances of is in the MAIN.PY the one which I've imported at the top. the class is called Customer()
0

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

4 Comments

I have tried for hours to wrap my head around it all but to no avail. so, here's the thing, allow me to put it like this, I want to find out how I can make several objects of one class in another file, and use all the made objects in other files as well.
in your code above, for objects of Customer are windows, correct?
@EmmanuelManoloSiame when do you want to create a new object on click of a button widget? If you want to access objects in other classes, you can pass the list to other classes as well. I didn't get what you meant by " for objects of Customer are windows "
I'd like for the objects to be created when the btn RENT A BIKE created, but I've already assigned a function that opens up a new window where the customer enters some values, lemme try to add more than one command to the button and ee what happens