扩展python模块

2024-09-27 21:28:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在PySimpleGUI库的扩展中工作。基本上,我会扩展(子类)一些类并将它们注入PySimpleGUI模块

是pythonic还是我应该离开原始模块,通过自己的扩展模块访问派生类?

这是我写的代码:

PySimpleGUIExt.py

import PySimpleGUI as sg


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

sg.TreeExt = TreeExt

示例.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()

我认为将派生类放在PySimpleGUI模块中会使事情变得更简单,因为我只是对所有类使用相同的模块

通常的方法可能是不注入类并像这样使用它们:

示例2.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()

Tags: 模块keypyimporttreedataassg

热门问题