alejandroautalan / pygubu-designer

A simple GUI designer for the python tkinter module
GNU General Public License v3.0
803 stars 98 forks source link

preserve manual code during rework #99

Closed matecsaj closed 5 months ago

matecsaj commented 2 years ago

The handwritten code I put in the generated .py file gets overwritten when making subsequent versions of my application. Reapplying my changes consumes labour and, more importantly, is an opportunity for error.

Could the generator insert tagged comments to delineate where manual code can go? Then when the code generator is re-run, could it preserve manually added code? If something is added, deleted or modified in the UI, the code generator will somehow need to cope.

Step 4 on this page is wiki page is relevant https://github.com/alejandroautalan/pygubu-designer/wiki/Design-Code-Iteration

A UML diagramming tool that I used 25-years ago generated C++ code with stubs where my handwritten code belonged. It has a feature they called round trip engineering.

A competing tool has this already; search for 'rework problem' here http://page.sourceforge.net

matecsaj commented 2 years ago

An alternate solution would be adding a tab in the Property pane to edit relevant Python code snippets; it would be handy if the objects in scope were available. Snippets would be inserted in the generated code and be indented appropriately. Both https://anvil.works and http://www.web2py.com take this approach. Rather than make a code editor and debugger from scratch, perhaps parts of another open-source project, such as IDLE could be reused.

matecsaj commented 2 years ago

Another alternate would be to transform pygubu into a plugin for Visual Studio, PyCharm, Eclipse, or other IDE with good Python support.

alejandroautalan commented 2 years ago

Hello.

These are all good ideas that could be implemented in the future.

Another option that can be implemented (and that would take less time) is to use inheritance. Consider the following development iteration:

Example of generated files:

myappbase.py:

#!/usr/bin/python3
import pathlib
import pygubu

PROJECT_PATH = pathlib.Path(__file__).parent
PROJECT_UI = PROJECT_PATH / "myapp.ui"

class MyappBase:
    def __init__(self, master=None):
        self.builder = builder = pygubu.Builder()
        builder.add_resource_path(PROJECT_PATH)
        builder.add_from_file(PROJECT_UI)
        self.mainwindow = builder.get_object("mainwindow", master)
        builder.connect_callbacks(self)

    def run(self):
        self.mainwindow.mainloop()

    def on_action1_clicked(self):
        pass

if __name__ == "__main__":
    app = MyappBase()
    app.run()

myapp.py:

#!/usr/bin/python3
from myappbase import MyappBase

class Myapp(MyappBase):

    def on_action1_clicked(self):
        # Write your code here.
        print('Button action 1 clicked.')

if __name__ == "__main__":
    app = Myapp()
    app.run()

After that, user can modify the interface and regenerate the base class and update the other file accordingly.

I think I saw this approach in another design app, but not remember which one. I'm using this approach for some projects. Let me know.

Regards Alejandro A.

matecsaj commented 2 years ago

I gave the inheritance approach a go, and it is working well so far; thanks for suggesting. Should the issue be closed after incorporating your advice into this wiki page?

alejandroautalan commented 2 years ago

Yes, please.

Regards Aejandro A.

matecsaj commented 2 years ago

While editing the wiki, I could not get the python code to look right. Would you please try pasting in the sample code that you provided earlier?

alejandroautalan commented 2 years ago

Added code to the wiki page.

Regards Alejandro A.

rdbende commented 2 years ago

Possibly the base class could be an ABC, and the methods to be overridden are abstractmethods, so the user has to implement them, that helps to avoid bugs and confusion.

alejandroautalan commented 5 months ago

Inheritance approach implemented in v0.39.