My program starts with a 'Loading_UI
' class that shows a simple loading bar and creates an object that contains data fetched from .csv files.
When the loading (= data fetching) is completed, the loading bar destroys itself (root.destroy()
) and then I want to pass on the object that I created to another class that will show the main UI of the program. To keep things neat, I want to call the main UI from its own .py file and own Main_UI
class.
Using the idea above, my question is: how can I pass the object on from the Loading_UI
class to the Main_UI
class?
(Run the program from RUN_loading.py
)
(DataBase.py
) Simple object creation:
class DataBase:
def __init__(self, name):
self.name = name
def show_content(self):
print(self.name)
And in the other .py file with class Loading_UI
, something like this:
(RUN_Loading.py
) UI with loading bar simulation:
from tkinter import *
from DataBase import DataBase
class Loading_UI:
def __init__(self, master):
self.master = master
self.DataBase = DataBase("This is our object.")
self.load_pct = DoubleVar()
self.load_pct.set(0)
loading_bar = Label(textvariable = self.load_pct)
loading_bar.grid()
self.handle_loading_progress()
def handle_loading_progress(self):
if self.load_pct.get() < 10:
self.load_pct.set(self.load_pct.get() + 1)
self.master.after(200, self.handle_loading_progress)
else:
self.show_main()
def show_main(self):
root.destroy()
from UI_Main import Main_UI
Main = Main_UI(self.DataBase)
Main.main()
root = Tk()
root.geometry("100x100+300+300")
app = Loading_UI(root)
root.mainloop()
And the main UI which would need to process the object would look like this.
(UI_Main.py) Main UI Window that pops up after loading is complete.
from tkinter import *
class Main_UI:
def __init__(self, database_object):
self.DataBase = database_object
# Run this function to check that the
# data object is properly received.
self.DataBase.show_content()
root = Tk()
app = Main_UI(root, database_object)
root.mainloop()
if __name__ == '__main__':
main()
The above is obviously wrong, but I just want to present the idea what I am after. Running the code above gives:
NameError: name 'database_object' is not defined
EDIT: Edited the code with a practical (simplified) example.