Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python OOP
#1
I am working on a GUI app using Tkinter, with the following file/folder structure
PythonGUI
|-BIKEGUI
|--ADDSTOCK
|---add_stock.py
|--RENTBIKE
|---rent_bike.py
|-main.py
|-bike_main_gui.py

"main.py" is where the all main classes and functionality of my program is,
I need to instantiate some classes from main.py and use them in other files(the other are just guis), how can I achieve this?
my aim is to instantiate a bike shop and uses all its values in other files.

PS. everything is written using OOP paradigm
Reply
#2
You need to show code and describe a concrete problem. It's very difficult to give any advice with very little detail like this.
Reply
#3
(Dec-27-2020, 01:18 PM)ndc85430 Wrote: You need to show code and describe a concrete problem. It's very difficult to give any advice with very little detail like this.

Thank you very much for your response,
here's the Link to my repo
Reply
#4
The syntax for doing this in python is very easy since you just create a python file and use it as a module in your main file. How you decide to build a file structure is the tricky part and it depends on how you want to think about your code. It's also a very personal decision as some coders love the Model-View-Controller paradigm and break things down into data, GUI and interfaces. Others would rather keep adding folders as they add new features so they might have folders like GUI, Database, Analysis, Reports, etc. This also depends on the App as well.

I suggest you just start simply with a sub-folder called "Classes" to get going. You can always add sub-folders later with little effort. Or, you could just name your classes with long strings that define their function. Remember, python doesn't care where the modules it imports are. That's all to help you and whoever reads your code so unless you have a paying job that defines how it must be done you can decide how much effort you want to put into it.

Once you have the file structure figured out you just write your classes in it and import them in the main code file. I'm doing a program that analyzes data from a batch of DUTs (Device Under Test) so I have a DUT_Data class in a DUT_DataClasses module. (It's very creative naming isn't it! Liar ) Right now it's not even in a sub-folder but I will expand the folder structure as things get more complicated.

In my main program I import the class with:
from DUT_DataClasses import DUT_Data as DD  # I use the 'as DD' to keep it short.
Then I just instantiate the Dut_Data objects (now called DD) and use the functionality inside.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020