问题
Since I discovered Python, I've created four little applications with Glade + Python + Gtk. For each of them, the structure is a folder, with the name of the app, containing :
- an "images" folder
- a main .py file which contains all classes that handle all project windows
- a .glade file which contains all windows (main app window, about dialog, config dialog, etc.)
- a style.css file
- (other files as sqlite file and/or json file if neccessary)
Now I'm wondering if this organization is a good practise. Because I've seen a video tutorial in which each window has its own ".ui" file and .py file. Also, other apps seem to be organized like this too and Anjuta projects seem to follow this rule too.
Furthermore, the tutorials that show how to create a Python-Gtk-Glade app are almost all one-window projects...
So my question is : how do you organize your Python-Gtk-Glade projects or what is the best way to do it?
Thanks in advance.
回答1:
The way you set up your apps will be dictated by your preferences (coding style), the size of the app, and the purpose of the app. However, here are a couple considerations:
Will total lines of Python code be greater than 1000 lines? A file with over a 1000 lines will be unwieldy to navigate. I know code folding, snippets, and etc help but still.
If you have more than one GtkWindow, put each window's .ui and .py separately. This helps when tracking down bugs or adding features. You mentioned sqlite, and sql queries within a file are easier to master 5 years down the road if the files are somewhat purpose specific. If you put dialogs in the same ui file as the parent window, it makes it easier to use set_transient_for and similar programming. Having more than one GtkWindow in a .ui file will make drag and drop widget reordering a pain.
Are parts of the app code reusable? If you find that you are duplicating features in several different files that have different purposes, make a class or function to simplify that one feature (only applicable to multi-file setup).
And finally, when your folder with .ui and .py files starts becoming large (? 25 - 50 files ?) you might want a subfolder that has a group of files with similar functionality. This is not written in stone. Nemo doesn't use this a lot, others do.
I actively develop my own business management software using Python + Gtk found here. I found the principles described above to work well for me.
I think the reason the documentation for Gtk typically shows single file setups is because they are merely proof of concept and not best app management tutorials. App management (for me, anyway) was trial and error, and from studying other open source apps out there.
Disclaimer: These suggestions are my opinion only. They are not endorsed by Python, Gtk, or Glade. You will need to evaluate these suggestions based on your use case.
来源:https://stackoverflow.com/questions/52485109/python-gtk-glade-app-structure-what-are-the-best-practises