diff --git a/TermTk/TTkUiTools/uiloader.py b/TermTk/TTkUiTools/uiloader.py index f751d7ed..5e962519 100644 --- a/TermTk/TTkUiTools/uiloader.py +++ b/TermTk/TTkUiTools/uiloader.py @@ -32,15 +32,42 @@ from TermTk.TTkTestWidgets import * from TermTk.TTkUiTools.uiproperties import TTkUiProperties class TTkUiLoader(): + '''TTkUiLoader + + .. _ttkDesigner: https://github.com/ceccopierangiolieugenio/pyTermTk/tree/main/ttkDesigner + + + `ttkdesigner Tutorial `_ + + ''' + @staticmethod - def loadFile(filePath, baseWidget:TTkWidget=None): + def loadFile(filePath, baseWidget:TTkWidget=None) -> TTkWidget: + '''load the file generated by ttkDesigner_ + + :param filePath: the file path + :type filePath: str + :param baseWidget: the custom widget that will be extended with this ui definition, if not defined a new :class:`~TermTk.TTkWidgets.widget.TTkWidget` will be returned,defaults to **None** + :type baseWidget: :class:`~TermTk.TTkWidgets.widget.TTkWidget`, optional + + :return: :class:`~TermTk.TTkWidgets.widget.TTkWidget` + ''' with open(filePath) as f: - return TTkUiLoader.loadJson(f.read(), TTkWidget) + return TTkUiLoader.loadJson(f.read(), baseWidget) return None @staticmethod - def loadJson(text, baseWidget:TTkWidget=None): - return TTkUiLoader.loadDict(json.loads(text), TTkWidget) + def loadJson(text, baseWidget:TTkWidget=None) -> TTkWidget: + '''load the json representing the ui definition of the widget + + :param text: the representation of the widget in Json format + :type text: json generated by ttkDesigner_ + :param baseWidget: the custom widget that will be extended with this ui definition, if not defined a new :class:`~TermTk.TTkWidgets.widget.TTkWidget` will be returned,defaults to **None** + :type baseWidget: :class:`~TermTk.TTkWidgets.widget.TTkWidget`, optional + + :return: :class:`~TermTk.TTkWidgets.widget.TTkWidget` + ''' + return TTkUiLoader.loadDict(json.loads(text), baseWidget) @staticmethod def _loadDict_1_0_0(ui, baseWidget:TTkWidget=None): @@ -193,7 +220,16 @@ class TTkUiLoader(): return widget @staticmethod - def loadDict(ui, baseWidget:TTkWidget=None): + def loadDict(ui, baseWidget:TTkWidget=None) -> TTkWidget: + '''load the dictionary representing the ui definition of the widget + + :param ui: the representation of the widget + :type ui: dictionary generated by ttkDesigner_ + :param baseWidget: the custom widget that will be extended with this ui definition, if not defined a new :class:`~TermTk.TTkWidgets.widget.TTkWidget` will be returned,defaults to **None** + :type baseWidget: :class:`~TermTk.TTkWidgets.widget.TTkWidget`, optional + + :return: :class:`~TermTk.TTkWidgets.widget.TTkWidget` + ''' cb = {'1.0.0' : TTkUiLoader._loadDict_1_0_0 }.get(ui['version'], None) if cb: diff --git a/tutorial/ttkDesigner/textEdit/README.rst b/tutorial/ttkDesigner/textEdit/README.rst index f8017f9e..bc0a7e95 100644 --- a/tutorial/ttkDesigner/textEdit/README.rst +++ b/tutorial/ttkDesigner/textEdit/README.rst @@ -16,6 +16,11 @@ .. _grid: https://ceccopierangiolieugenio.github.io/pyTermTk/autogen.TermTk/TermTk.TTkLayouts.gridlayout.html#ttkgridlayout .. _TTkGridLayout: https://ceccopierangiolieugenio.github.io/pyTermTk/autogen.TermTk/TermTk.TTkLayouts.gridlayout.html#ttkgridlayout +.. _TTkUILoader: https://ceccopierangiolieugenio.github.io/pyTermTk/autogen.TermTk/TermTk.TTkUiTools.uiloader.html#TermTk.TTkUiTools.uiloader.TTkUiLoader + +.. contents:: + + =================== ttkDesigner_ - Your first TextEditor =================== @@ -169,8 +174,15 @@ Exported: `texteditor.01.py `__ @@ -211,3 +223,47 @@ Once (quick)exported the code, we need to define the appropriate routines and li # Connect the save routine to the (save)"filePicked" event btnSave.filePicked.connect(saveRoutine) + +Option 2) Extend a custom widget including the open/save methods +---------------------------------------------------------------- + +`texteditor.03.py `_ +(`Try Online `__) + +.. code:: bash + + # If you want to try without installation, run from the pyTermTk root folder + PYTHONPATH=`pwd` python3 tutorial/ttkDesigner/textEdit/texteditor.03.py + + +.. code:: python + + class MyTextEditor(TTkWindow): + def __init__(self): + # The "TTkUiLoader" is responsible to init this custom object + # and extend it to the "textEditWindow" created in this tutorial + # NOTE: no "super().__init__()" is required + TTkUiLoader.loadDict(TTkUtil.base64_deflate_2_obj( + # + ), self) + + # Connect the open routine to the (open)"filePicked" event + self.getWidgetByName("BtnOpen").filePicked.connect(self.openRoutine) + # Connect the save routine to the (save)"filePicked" event + self.getWidgetByName("BtnSave").filePicked.connect(self.saveRoutine) + + # This is a generic routine to open/read a file + # and push the content to the "TextEdit" widget + pyTTkSlot(str) + def openRoutine(self, fileName): + textEdit = self.getWidgetByName("TextEdit") + with open(fileName) as fp: + textEdit.setText(fp.read()) + + # This is a generic routine to save the content of + # the "TextEdit" widget to the chosen file + pyTTkSlot(str) + def saveRoutine(self, fileName): + textEdit = self.getWidgetByName("TextEdit") + with open(fileName, 'w') as fp: + fp.write(textEdit.toPlainText()) diff --git a/tutorial/ttkDesigner/textEdit/texteditor.03.py b/tutorial/ttkDesigner/textEdit/texteditor.03.py new file mode 100755 index 00000000..5997b2f2 --- /dev/null +++ b/tutorial/ttkDesigner/textEdit/texteditor.03.py @@ -0,0 +1,48 @@ +from TermTk import TTkUtil, TTkUiLoader, TTk, TTkWindow +from TermTk import pyTTkSlot + + +class MyTextEditor(TTkWindow): + def __init__(self): + # The "TTkUiLoader" is responsible to init this custom object + # and extend it to the "textEditWindow" created in this tutorial + # NOTE: no "super().__init__()" is required + TTkUiLoader.loadDict(TTkUtil.base64_deflate_2_obj( + "eJytVltvG0UUXtvrXV96yYVSICBWFVIdJCwHoRLUvCSmMXTr1koMFarysNkd+Yy63rV2Z0ODVKniyalGPMCg8FYBggd+AU/8pv4Ezsyur0lQEuGV5T3znTnzfd/c/EL/" + + "+ZuKpj7PRY2bBySKaRgIXlyrN+oNwQssoUJCRdd34ljwcrf79DENvPBbwY2BEzn9WOH6Q6dPBL+KWJc8Y/c8ysJI8FInjCmTJfdEzdbtPOH6Lv2OqPBL+wbh5TYNrMfU" + + "YyDsPK/I6AtCe8CErSPoPMvAdn4BUQwztK0vcLPjeB4Neqpawc7Jh3DjgXMYJgzJINdWRL1RbH5NY7rvE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prgBD0kN1qpiuQSalfRVHNW5GWm6qYrm3eJ" + + "j6ZfvDv8jWkVTNsO3SSWFElS/xfnLV9T"), self) + + # Connect the open routine to the (open)"filePicked" event + self.getWidgetByName("BtnOpen").filePicked.connect(self.openRoutine) + # Connect the save routine to the (save)"filePicked" event + self.getWidgetByName("BtnSave").filePicked.connect(self.saveRoutine) + + # This is a generic routine to open/read a file + # and push the content to the "TextEdit" widget + pyTTkSlot(str) + def openRoutine(self, fileName): + textEdit = self.getWidgetByName("TextEdit") + with open(fileName) as fp: + textEdit.setText(fp.read()) + + # This is a generic routine to save the content of + # the "TextEdit" widget to the chosen file + pyTTkSlot(str) + def saveRoutine(self, fileName): + textEdit = self.getWidgetByName("TextEdit") + with open(fileName, 'w') as fp: + fp.write(textEdit.toPlainText()) + +# Initialize TTK, add the window widget, and start the main loop +root=TTk() +root.layout().addWidget(MyTextEditor()) +root.mainloop()