1

I am working in an extension of the library PySimpleGUI. Basically I would extend(subclass) some classes and inject them in the PySimpleGUI module.

Is that pythonic or should I leave alone the original module and access the derived classes through my own extending module?

This is the code I wrote:

PySimpleGUIExt.py

import PySimpleGUI as sg


class TreeExt(sg.Tree):
    def insert_item(self, parent, key, text, values, icon=None):
        #...
        pass

sg.TreeExt = TreeExt

example.py

from PySimpleGUIExt import sg 


data = sg.TreeData()           # using base class
tree = sg.TreeExt(data, headings=['Column1'])  # using extended class!
win = sg.Window('title', [[tree]])
win.finalize()
tree.insert_item('', 'key', 'text', ['value1'])  # using new method!
win.read()

I think that putting the derived class in the PySimpleGUI module makes things simpler because I just use the same module for all classes.

The normal approach would be maybe not to inject the classes and use them like this:

example2.py

import extendPySimpleGUI as esg
import PySimpleGUI as sg


data = sg.TreeData()  # using base class
tree = sge.TreeExt(data, headings=['Column1])  # using extended class!
win = sg.Window('title', [[tree]])     # using base class
win.finalize()
tree.insert_item('', 'key', 'text', ['value1'])    # using new method!
win.read()
nadapez
  • 2,603
  • 2
  • 20
  • 26
  • 2
    "pythonic" is in the eye of the beholder. That said, [monkey-patching](https://stackoverflow.com/questions/5626193/what-is-monkey-patching) is a fairly well-know approach (although probably not used all that often). Its biggest weakness probably is that it's inherently fragile. – martineau Mar 19 '21 at 18:21
  • Another method would be to `from PySimpleGUI import *` in `extendPySimpleGUI`, then `import extendPySimpleGUI as sg` and use _that_ `sg` for all classes. – cco Mar 19 '21 at 19:23

0 Answers0