Community
    • Login

    Little Dialog-wrapper for PythonScript

    Scheduled Pinned Locked Moved Notepad++ & Plugin Development
    74 Posts 8 Posters 13.1k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Alan KilbornA
      Alan Kilborn @Ekopalypse
      last edited by

      @Ekopalypse said in Little Dialog-wrapper for PythonScript:

      If you need to add an additional control at a later time, then all you have to do is copy the newly generated line.

      Sure, easy enough for one control, one line.
      I was more thinking about how I tend to do things… on a greater than 1 line basis. :-)

      but that is already the case today as well.

      Today we don’t have to think about script concurrency, because PS mostly prevents it. Anyway, just thought I’d raise the question.

      EkopalypseE 1 Reply Last reply Reply Quote 0
      • EkopalypseE
        Ekopalypse @Alan Kilborn
        last edited by

        @Alan-Kilborn

        oh no, it’s not a concurrency issue, it’s more like the callbacks we use today - event driven.
        The script starts the dialog and ends. But the class that manages the dialog stays active until the dialog is closed.

        1 Reply Last reply Reply Quote 3
        • Michael VincentM
          Michael Vincent @Ekopalypse
          last edited by

          @Ekopalypse said in Little Dialog-wrapper for PythonScript:

          And then I thought, what if … trailer … :-D

          Any alpha / beta version ready to test yet? I keep checking your repo but don’t see any new commits. I have a pretty easy use case in mind - an edit box with checkboxes for regex and case insensitive - all for a filter search lifted from @Alan-Kilborn 's work!

          Integrating my supreme Notepad++ experience by standing on the shoulders of giants!

          Cheers.

          EkopalypseE 2 Replies Last reply Reply Quote 2
          • EkopalypseE
            Ekopalypse @Michael Vincent
            last edited by

            @Michael-Vincent

            Hi, I have quite a lot on my plate privately at the moment, but I promise to upload a new version this weekend.
            It’s actually going quite well and if you stick to what’s currently implemented, there shouldn’t be any difficulties. Sorry for the delays.

            1 Reply Last reply Reply Quote 2
            • EkopalypseE
              Ekopalypse @Michael Vincent
              last edited by

              @Michael-Vincent

              Repo has been updated, but be warned that I’m not completely satisfied yet, which means it’s not API stable. Further updates could break things that currently work.

              EkopalypseE 1 Reply Last reply Reply Quote 1
              • EkopalypseE
                Ekopalypse @Ekopalypse
                last edited by Ekopalypse

                @Michael-Vincent

                There was a typo that prevented the dialog from being created.
                I should have tested it before, sorry. Is fixed now.

                Michael VincentM 1 Reply Last reply Reply Quote 1
                • Michael VincentM
                  Michael Vincent @Ekopalypse
                  last edited by

                  @Ekopalypse

                  Great! I have my small example:

                  from WinDialog import Button, Dialog, CheckBoxButton, Label, TextBox
                  from WinDialog.controls.button import BM, BST
                  from WinDialog.win_helper import SendMessage
                  
                  user_input = ''
                  REGEX      = True
                  IGNORECASE = True
                  
                  class FilerLinesEditDlg(Dialog):
                      def __init__(self):
                          super().__init__(            title='Filter Lines Editing', size=(250, 75), center = True)
                          self.label  = Label(         title='Filter for:'         , position=(10, 12),  size=(30, 11) )
                          self.edit   = TextBox(                                     position=(45, 10),  size=(195, 14) )
                          self.case   = CheckBoxButton(title='Case Sensitive'      , position=(45, 30),  size=(80, 14)  )
                          self.regex  = CheckBoxButton(title='Regular Expression'  , position=(145, 30), size=(80, 14)  )
                          self.ok     = Button(        title='OK'                  , position=(130, 55), size=(50, 11)  )
                          self.cancel = Button(        title='Cancel'              , position=(187, 55), size=(50, 11)  )
                  
                          self.ok.on_click     = self.on_ok
                          self.cancel.on_click = self.on_cancel
                          self.case.on_click   = self.on_case
                          self.regex.on_click  = self.on_regex
                          self.show()
                  
                      def initialize(self):
                          global IGNORECASE
                          global REGEX
                          SendMessage(self.case.hwnd, BM.SETCHECK, IGNORECASE, 0)
                          SendMessage(self.regex.hwnd, BM.SETCHECK, REGEX, 0)
                  
                      def on_ok(self):
                          global user_input
                          user_input = self.edit.get_text()
                          self.terminate()
                  
                      def on_cancel(self):
                          global user_input
                          user_input = None
                          self.terminate()
                  
                      def on_case(self):
                          global IGNORECASE
                          check = SendMessage(self.case.hwnd, BM.GETCHECK, 0, 0)
                          if check & BST.CHECKED:
                              IGNORECASE = True
                          else:
                              IGNORECASE = False
                  
                      def on_regex(self):
                          global REGEX
                          check = SendMessage(self.regex.hwnd, BM.GETCHECK, 0, 0)
                          if check & BST.CHECKED:
                              REGEX = True
                          else:
                              REGEX = False
                  
                  
                  FilerLinesEditDlg()
                  
                  print(f"User:  {user_input}")
                  print(f"Case:  {IGNORECASE}")
                  print(f"Regex: {REGEX}")
                  

                  It seems to run correctly and prints status to the PythonScript console. I plan to integrate this into a larger script.

                  I’m not sure I did the checkbox stuff correctly. It works, but I had to import some extra classes and the SendMessage function from your helper library. Did I do this right?

                  Cheers.

                  EkopalypseE 1 Reply Last reply Reply Quote 2
                  • EkopalypseE
                    Ekopalypse @Michael Vincent
                    last edited by

                    @Michael-Vincent

                    You can do it this way, but I personally would put the globals as attributes in the class to avoid globals in the first place.
                    For the GETCHECK, because the checkbox is a “boolean” value, I would do something like: …

                    ...
                        def on_case(self):
                            self.IGNORECASE = not self.IGNORECASE
                    ...
                    dlg = FilerLinesEditDlg()
                    
                    print(f"User:  {dlg.user_input}")
                    print(f"Case:  {dlg.IGNORECASE}")
                    print(f"Regex: {dlg.REGEX}")
                    

                    As for the SETCHECK, the SendMessage function is currently required.
                    The next version, uploaded this weekend, will have a checkbox button method setCheckState (that’s the same kind of naming PS does … so it seems to make the most sense, right?). And now that I think about it, getCheckState makes sense too because of the 3-state checkboxes. Also, the next version will be able to handle IDOK and IDCANCEL, which means you can close a dialog with ESC and trigger the execution of the edit with ENTER.

                    Michael VincentM 1 Reply Last reply Reply Quote 3
                    • Michael VincentM
                      Michael Vincent @Ekopalypse
                      last edited by Michael Vincent

                      @Ekopalypse said in Little Dialog-wrapper for PythonScript:

                      will have a checkbox button method setCheckState (that’s the same kind of naming PS does … so it seems to make the most sense, right?). And now that I think about it, getCheckState makes sense too because of the 3-state checkboxes. Also, the next version will be able to handle IDOK and IDCANCEL, which means you can close a dialog with ESC and trigger the execution of the edit with ENTER.

                      This just keeps getting better!

                      I updated the globals to a return class that I pass in and then examine on output. The issue is I want this as a prompt in a larger already existing PythonScript and class so the “globals” are just globals in this example - in my project, they are attributes of the existing class that the dialog will need to know to set on startup and then return in case of modifications in the dialog. The globals were just a “cheat” way to illustrate a “complete self-contained example” to post here. But point taken - I am avoiding globals in the finished product.

                      The on_case recommendation works as well very nicely and certainly simplifies code - removes the need for me obtaining BM.GETCHECK as well.

                      It’s Memorial Day weekend in the States so I’ll be stepping away from all this for the long weekend. I do hope you take time to relax this - and any weekend for that matter. I’m in no rush for the updates, but will keep checking back the repo to see when they in fact do arrive.

                      UPDATE:
                      The updated script from above with fixes discussed. Also add the c#_ prefixes to the controls to force the TABSTOP order - seems to be alphabetical:

                      from Npp import editor
                      
                      #----------
                      
                      from WinDialog import Button, CheckBoxButton, DefaultButton, Dialog, Label, TextBox
                      from WinDialog.controls.button import BM
                      from WinDialog.win_helper import SendMessage
                      
                      #----------
                      
                      class Returns(object):
                          def __init__(self, U=None, I=False, R=False):
                              self.user_input = U
                              self.IGNORECASE = I
                              self.REGEX      = R
                      
                      class FilerLinesEditDlg(Dialog):
                          def __init__(self, ret=Returns()):
                              super().__init__(               title='Filter Lines Editing', center = True,      size=(250, 75) )
                              self.label     = Label(         title='Filter for:'         , position=(10, 12),  size=(30, 11)  )
                              self.c2_edit   = TextBox(                                     position=(45, 10),  size=(195, 14) )
                              self.c3_case   = CheckBoxButton(title='Case Sensitive'      , position=(45, 30),  size=(80, 14)  )
                              self.c4_regex  = CheckBoxButton(title='Regular Expression'  , position=(145, 30), size=(80, 14)  )
                              self.c1_ok     = DefaultButton( title='OK'                  , position=(130, 55), size=(50, 11)  )
                              self.c5_cancel = Button(        title='Cancel'              , position=(187, 55), size=(50, 11)  )
                      
                              self.ret = ret
                      
                              self.c1_ok.on_click     = self.on_ok
                              self.c5_cancel.on_click = self.on_cancel
                              self.c3_case.on_click   = self.on_case
                              self.c4_regex.on_click  = self.on_regex
                      
                              self.show()
                      
                          def initialize(self):
                              self.c2_edit.set_text(self.ret.user_input)
                              SendMessage(self.c3_case.hwnd,  BM.SETCHECK, self.ret.IGNORECASE, 0)
                              SendMessage(self.c4_regex.hwnd, BM.SETCHECK, self.ret.REGEX, 0)
                      
                          def on_ok(self):
                              self.ret.user_input = self.c2_edit.get_text()
                              self.terminate()
                      
                          def on_cancel(self):
                              self.ret.user_input = None
                              self.terminate()
                      
                          def on_case(self):
                              self.ret.IGNORECASE = not self.ret.IGNORECASE
                      
                          def on_regex(self):
                              self.ret.REGEX = not self.ret.REGEX
                      
                      #-----^^^^-----
                      
                      user_input = editor.getSelText()
                      IGNORECASE = False
                      REGEX      = True
                      
                      #-----vvvv-----
                      ret        = Returns(user_input, IGNORECASE, REGEX)
                      FilerLinesEditDlg(ret)
                      user_input = ret.user_input
                      IGNORECASE = ret.IGNORECASE
                      REGEX      = ret.REGEX
                      #-----^^^^-----
                      
                      if ret.user_input is None:
                          print("EXIT")
                      else:
                          print(f"User:  {user_input}")
                          print(f"Case:  {IGNORECASE}")
                          print(f"Regex: {REGEX}")
                      

                      Cheers.

                      EkopalypseE Alan KilbornA 3 Replies Last reply Reply Quote 3
                      • EkopalypseE
                        Ekopalypse @Michael Vincent
                        last edited by

                        @Michael-Vincent

                        Don’t worry, the weekends are for family commitments. I’ll just publish what I’ve managed to do during the week.
                        As for the WS_TABSTOP, I thought that the order of creation of the controls determines the order … I’ll have to check that.

                        1 Reply Last reply Reply Quote 1
                        • Alan KilbornA
                          Alan Kilborn @Michael Vincent
                          last edited by

                          @Michael-Vincent said in Little Dialog-wrapper for PythonScript:

                          Also add the c#_ prefixes to the controls to force the TABSTOP order

                          Confused me at first; I’m wondering what the heck C# has to do with this…

                          Finally I notice this is what you meant:

                          9d48c666-0250-41fb-8447-c0c4d1287ab3-image.png

                          1 Reply Last reply Reply Quote 2
                          • EkopalypseE
                            Ekopalypse @Michael Vincent
                            last edited by

                            @Michael-Vincent

                            The issue is here. dir always returns a sorted list.

                            Replacing it with for item in self.__dict__.keys(): seems to behave as expected. The first control created that has the style ws_tabstop gets the initial focus.

                            EkopalypseE 1 Reply Last reply Reply Quote 1
                            • EkopalypseE
                              Ekopalypse @Ekopalypse
                              last edited by

                              First beta version released.
                              Sorry for breaking existing code, but from now on this should not happen.

                              The current state of the project is what I currently support.
                              If you encounter issues where the dialog box or controls do not work as described or advertised,
                              please report them by submitting a “git issue”. I will diligently investigate the issue and work on a solution.

                              However, please note that any requests for additional functionality beyond what is currently offered will be considered feature requests.
                              While I appreciate user feedback, I prioritize implementing features that benefit me personally and align with the overall goals of the project,
                              which is to be a simple wrapper over DialogBoxIndirectParam Windows API.
                              Thank you for your understanding."

                              Michael VincentM 1 Reply Last reply Reply Quote 3
                              • Michael VincentM
                                Michael Vincent @Ekopalypse
                                last edited by

                                @Ekopalypse said in Little Dialog-wrapper for PythonScript:

                                First beta version released.

                                My example script above borrows from @Alan-Kilborn 's filter line editing script and provides a nice GUI to select case insensitive or regular expression type filtering.

                                Here’s a new example, inspired by a script in the jN plugin translate script:

                                import requests
                                
                                from enum import Enum
                                
                                from Npp import editor
                                
                                from WinDialog import Button, ComboBox, DefaultButton, Dialog, Label, TextBox
                                from WinDialog.win_helper import WindowStyle as WS
                                
                                TITLE = "Translate"
                                
                                class Languages(Enum):
                                    """Translated language options."""
                                    Chinese    = "zh"
                                    English    = "en"
                                    French     = "fr"
                                    German     = "de"
                                    Italian    = "it"
                                    Japanese   = "ja"
                                    Portuguese = "pt"
                                    Russian    = "ru"
                                    Spanish    = "es"
                                
                                class Returns(object):
                                    """The input / output for the Translator service."""
                                    def __init__(self, text="", srclang="English", dstlang="English"):
                                        self.text = text
                                        self.trans = ""
                                        self.srclang = srclang
                                        self.dstlang = dstlang
                                
                                class Translator(Dialog):
                                    """A Translator dialog interface."""
                                    def __init__(self, ret=Returns()):
                                        super().__init__(               title=TITLE         , center = True      , size=(250, 140) )
                                        self.translate = DefaultButton( title='&Translate'  , position=(80, 120), size=(50, 11)  )
                                        self.label1    = Label(         title='Text:'       , position=(10, 12)  , size=(35, 11)  )
                                        self.text      = TextBox(                             position=(45, 10)  , size=(195, 44) )
                                        self.swapt     = Button(        title='^&v'         , position=(20, 55)  , size=(20, 14)  )
                                        self.srclang   = ComboBox(                            position=(45, 56)  , size=(80, 14)  )
                                        self.swapl     = Button(        title='<&=>'        , position=(132, 55) , size=(20, 14)  )
                                        self.dstlang   = ComboBox(                            position=(160, 56) , size=(80, 14)  )
                                        self.label2    = Label(         title='Translated:' , position=(10, 72)  , size=(35, 11)  )
                                        self.trans     = TextBox(                             position=(45, 70)  , size=(195, 44) )
                                        self.replace   = Button(        title='&Replace'    , position=(135, 120) , size=(50, 11)  )
                                        self.close     = Button(        title='&Close'      , position=(190, 120), size=(50, 11)  )
                                
                                        self.ret = ret
                                
                                        self.onIdOk             = self.on_translate
                                        self.translate.onClick  = self.on_translate
                                        self.swapt.onClick      = self.on_swapt
                                        self.swapl.onClick      = self.on_swapl
                                        self.dstlang.onSelEndOk = self.on_translate
                                        self.replace.onClick    = self.on_replace
                                        self.close.onClick      = self.on_close
                                
                                        self.srclang.style = self.dstlang.style | WS.TABSTOP
                                        self.dstlang.style = self.dstlang.style | WS.TABSTOP
                                
                                        self.show()
                                
                                    def initialize(self):
                                        """Initialize the dialog."""
                                        self.text.setText(self.ret.text)
                                        self._init_langs()
                                
                                    def _init_langs(self):
                                        srclang = list(n.name for n in Languages)
                                        if self.ret.srclang in srclang:
                                            srclang.insert(0, self.ret.srclang)
                                        self.srclang.set(srclang)
                                
                                        dstlang = list(n.name for n in Languages)
                                        if self.ret.dstlang in dstlang:
                                            dstlang.insert(0, self.ret.dstlang)
                                        self.dstlang.set(dstlang)
                                
                                    def on_translate(self):
                                        """Translate the text."""
                                        text_encoded = requests.utils.quote(self.text.getText())
                                
                                        srclang = Languages[self.srclang.getSelectedItemText()]
                                        dstlang = Languages[self.dstlang.getSelectedItemText()]
                                
                                        # Set return languages
                                        self.ret.srclang = srclang.name
                                        self.ret.dstlang = dstlang.name
                                
                                        srccode = srclang.value
                                        dstcode = dstlang.value
                                        # EXAMPLE: LANGPAIR=EN|IT USING 2 LETTER ISO OR RFC3066 LIKE ZH-CN
                                        langpair = f"{srccode}|{dstcode}"
                                
                                        r = requests.get(f"http://mymemory.translated.net/api/get?q={text_encoded}&langpair={langpair}")
                                        response = r.json()['responseData']['translatedText']
                                
                                        # Set return translation
                                        if response is not None:
                                            self.ret.trans = response
                                        else:
                                            self.ret.trans = "(no translation found)"
                                        self.trans.setText(self.ret.trans)
                                
                                        # Set return text
                                        self.ret.text = self.text.getText()
                                
                                    def on_swapl(self):
                                        """Swap languages."""
                                        self.ret.dstlang = self.srclang.getSelectedItemText()
                                        self.ret.srclang = self.dstlang.getSelectedItemText()
                                        self._init_langs()
                                
                                    def on_swapt(self):
                                        """Swap texts."""
                                        self.ret.trans = self.text.getText()
                                        self.ret.text  = self.trans.getText()
                                        self.text.setText(self.ret.text)
                                        self.trans.setText(self.ret.trans)
                                
                                    def on_replace(self):
                                        """Replace text with translation in document."""
                                        if self.ret.trans != "":
                                            editor.replaceSel(self.ret.trans)
                                            self.terminate()
                                
                                    def on_close(self):
                                        """Close dialog."""
                                        self.terminate()
                                
                                
                                class Translate():
                                    """
                                    A translator service.
                                    """
                                    def __init__(self):
                                        self.text    = ""
                                        self.trans   = ""
                                        self.srclang = "English"
                                        self.dstlang = "English"
                                
                                    def translate(self):
                                        text = editor.getSelText()
                                        if text is not None:
                                            self.text = text
                                
                                        ret = Returns(self.text, self.srclang, self.dstlang)
                                        Translator(ret)
                                        self.text    = ret.text
                                        self.trans   = ret.trans
                                        self.srclang = ret.srclang
                                        self.dstlang = ret.dstlang
                                
                                if __name__ == '__main__':
                                    try:
                                        isinstance(translate, Translate)
                                        # print("Translator `translate' already enabled")
                                    except NameError:
                                        translate = Translate()
                                
                                    translate.translate()
                                

                                173b2676-3705-49cd-af4d-f5ba18dead0a-image.png

                                Cheers.

                                Michael VincentM 1 Reply Last reply Reply Quote 3
                                • Michael VincentM
                                  Michael Vincent @Michael Vincent
                                  last edited by

                                  @Michael-Vincent said in Little Dialog-wrapper for PythonScript:

                                  Here’s a new example

                                  And a dictionary as well:

                                  import requests
                                  
                                  from Npp import editor
                                  
                                  from WinDialog import Button, DefaultButton, Dialog, Label, ListBox, TextBox
                                  from WinDialog.win_helper import WindowStyle as WS
                                  
                                  TITLE = "Dictionary"
                                  
                                  class Returns(object):
                                      """The input / output for the Dictionary service."""
                                      def __init__(self, word=""):
                                          self.word = word
                                          self.definition = ""
                                          self.synonyms = []
                                          self.antonyms = []
                                          self.replace = None
                                  
                                  class Dictionary(Dialog):
                                      """A Dictionary dialog interface."""
                                      def __init__(self, ret=Returns()):
                                          super().__init__(                title=TITLE      , center = True      , size=(220, 250))
                                          self.word       = TextBox(                          position=(10, 12)  , size=(150, 14) )
                                          self.lookup     = DefaultButton( title='&Lookup'  , position=(165, 13) , size=(45, 11)  )
                                          self.definition = TextBox(                          position=(10, 30)  , size=(200, 100))
                                          self.label1     = Label(         title='Synonyms' , position=(10, 140) , size=(45, 11)  )
                                          self.synonyms   = ListBox(                          position=(10, 155) , size=(90, 65)  )
                                          self.replsyn    = Button(        title='Re&place' , position=(10, 220),  size=(45, 11)  )
                                          self.label2     = Label(         title='Antonyms' , position=(120, 140), size=(45, 11)  )
                                          self.antonyms   = ListBox(                          position=(120, 155), size=(90, 65)  )
                                          self.replant    = Button(        title='Repl&ace' , position=(120, 220), size=(45, 11)  )
                                          self.close      = Button(        title='&Close'   , position=(165, 235), size=(45, 11)  )
                                  
                                          self.ret = ret
                                  
                                          self.onIdOk          = self.on_lookup
                                          self.lookup.onClick  = self.on_lookup
                                          self.replsyn.onClick = self.on_replace_syn
                                          self.replant.onClick = self.on_replace_ant
                                          self.close.onClick   = self.on_close
                                  
                                          self.definition.style = self.definition.style | WS.VSCROLL | WS.HSCROLL # | WS.DISABLED
                                          self.synonyms.style   = self.synonyms.style   | WS.TABSTOP
                                          self.antonyms.style   = self.antonyms.style   | WS.TABSTOP
                                  
                                          self.show()
                                  
                                      def _initialize(self):
                                          self.ret = Returns(self.ret.word)
                                  
                                          self.word.setText(self.ret.word)
                                          self.synonyms.clear()
                                          self.antonyms.clear()
                                  
                                      def initialize(self):
                                          """Initialize the dialog."""
                                          self._on_lookup()
                                  
                                      def _on_lookup(self):
                                          """Lookup the word."""
                                          self._initialize()
                                  
                                          text_encoded = requests.utils.quote(self.word.getText())
                                          r = requests.get(f"http://api.dictionaryapi.dev/api/v2/entries/en/{text_encoded}")
                                          if r.status_code != 200:
                                              return
                                  
                                          synonyms = []
                                          antonyms = []
                                          response = ""
                                          for idx, defs in enumerate(r.json()[0]['meanings']):
                                              response += f"{idx+1} : {defs['partOfSpeech']}\r\n"
                                              for pos in defs['definitions']:
                                                  response += f"    {pos['definition']}\r\n"
                                                  synonyms.extend(pos['synonyms'])
                                                  antonyms.extend(pos['antonyms'])
                                              synonyms.extend(defs['synonyms'])
                                              antonyms.extend(defs['antonyms'])
                                  
                                          self.ret.definition = response
                                          self.definition.setText(self.ret.definition)
                                  
                                          # Need case insensitive since ListBox has style SORT, which is case insensitive
                                          self.ret.synonyms = sorted(set(synonyms), key=str.casefold)
                                          self.ret.antonyms = sorted(set(antonyms), key=str.casefold)
                                          self.synonyms.addStrings(self.ret.synonyms)
                                          self.antonyms.addStrings(self.ret.antonyms)
                                  
                                      def on_lookup(self):
                                          self.ret.word = self.word.getText()
                                          self._on_lookup()
                                  
                                      def on_replace_syn(self):
                                          item = self.synonyms.getSelectedItem()
                                          if item < 0:
                                              return
                                  
                                          self.ret.replace = self.synonyms._ListBox__items[item].value
                                          editor.replaceSel(self.ret.replace)
                                          self.terminate()
                                  
                                      def on_replace_ant(self):
                                          item = self.antonyms.getSelectedItem()
                                          if item < 0:
                                              return
                                  
                                          self.ret.replace = self.antonyms._ListBox__items[item].value
                                          editor.replaceSel(self.ret.replace)
                                          self.terminate()
                                  
                                      def on_close(self):
                                          """Close dialog."""
                                          self.terminate()
                                  
                                  def editor_getWordAtCaretOrSelection():
                                      retval = ''
                                      (sel_start, sel_end) = (editor.getSelectionStart(), editor.getSelectionEnd())
                                      if editor.getSelections() == 1 and sel_start != sel_end:
                                          retval = editor.getTextRange(sel_start, sel_end)
                                      else:
                                          start_of_word_pos = editor.wordStartPosition(editor.getCurrentPos(), True)
                                          end_of_word_pos = editor.wordEndPosition(start_of_word_pos, True)
                                          if start_of_word_pos != end_of_word_pos:
                                              retval = editor.getTextRange(start_of_word_pos, end_of_word_pos)
                                              editor.setSelection(end_of_word_pos, start_of_word_pos)
                                      return retval
                                  
                                  def lookup():
                                      word = ""
                                      if editor.getSelectionEmpty():
                                          word = editor_getWordAtCaretOrSelection()
                                      else:
                                          word = editor.getSelText()
                                  
                                      if len(word) <= 0:
                                          return
                                  
                                      ret = Returns(word)
                                      Dictionary(ret)
                                      # print(ret.word)
                                      # print(ret.definition)
                                      # print(ret.synonyms)
                                      # print(ret.antonyms)
                                      # print(ret.replace)
                                  
                                  if __name__ == '__main__':
                                      lookup()
                                  

                                  21186d37-9f53-47c5-96c6-1ecd66506f89-image.png

                                  Cheers.

                                  1 Reply Last reply Reply Quote 2
                                  • Alan KilbornA
                                    Alan Kilborn @Michael Vincent
                                    last edited by Alan Kilborn

                                    I’m just starting to experiment with this “Little Dialog wrapper”…


                                    @Michael-Vincent said in Little Dialog-wrapper for PythonScript:

                                    nice demo in WinDialog_tests_\test_win_dialog.py

                                    Actually, I think, from the screenshot, that it is ...\lib\WinDialog\__tests__\test_button.py that is being run, NOT test_win_dialog.py.


                                    When trying either of the two basic examples HERE, I get a dialog that appears and looks like this:

                                    eec693b9-2698-4c62-be5d-f4dc0b88f370-image.png

                                    But clicking on the Okay button doesn’t print anything to the PS console window (as the code makes me believe it should) and clicking on the Close Dialog button doesn’t close it (only clicking on the X in the upper right corner will end it).

                                    I must be doing something wrong in a really basic sense?


                                    @Michael-Vincent

                                    I tried your recent (August 2023) scripts and both of them failed because they can’t find the import when you do import requests. Probably you are running with Prefer installed Python libraries?

                                    I don’t have an “installed” Python, but I have a portable Python 3.11.4. I wonder what it takes to make Notepad++ see and use that…

                                    rdipardoR EkopalypseE 2 Replies Last reply Reply Quote 2
                                    • rdipardoR
                                      rdipardo @Alan Kilborn
                                      last edited by

                                      @Alan-Kilborn said in Little Dialog-wrapper for PythonScript:


                                      @Michael-Vincent

                                      I tried your recent (August 2023) scripts and both of them failed because they can’t find the import when you do import requests.

                                      The builtin urllib.request module should be available to any Python 3 host. You can then do the JSON serialization with the builtin json module. I can’t test it at the moment, but a more portable script would look something like this:

                                      -import requests
                                      +import json
                                      +import urllib.request as requests
                                      +import urllib.parse
                                      # . . .
                                       from Npp import editor
                                      
                                               """Lookup the word."""
                                               self._initialize()
                                      
                                      -        text_encoded = requests.utils.quote(self.word.getText())
                                      -        r = requests.get(f"http://api.dictionaryapi.dev/api/v2/entries/en/{text_encoded}")
                                      -        if r.status_code != 200:
                                      +        text_encoded = urllib.parse.quote(self.word.getText())
                                      +        r = requests.urlopen(requests.Request(f"http://api.dictionaryapi.dev/api/v2/entries/en/{text_encoded}"))
                                      +        if r.status != 200:
                                      +            r.close()
                                                   return
                                      
                                               synonyms = []
                                               antonyms = []
                                               response = ""
                                      -        for idx, defs in enumerate(r.json()[0]['meanings']):
                                      +        for idx, defs in enumerate(json.loads(r.read().decode('utf8'))[0]['meanings']):
                                                   response += f"{idx+1} : {defs['partOfSpeech']}\r\n"
                                                   for pos in defs['definitions']:
                                                       response += f"    {pos['definition']}\r\n"
                                      # . . .
                                               self.ret.antonyms = sorted(set(antonyms), key=str.casefold)
                                               self.synonyms.addStrings(self.ret.synonyms)
                                               self.antonyms.addStrings(self.ret.antonyms)
                                      +        r.close()
                                      
                                      Alan KilbornA 1 Reply Last reply Reply Quote 1
                                      • EkopalypseE
                                        Ekopalypse @Alan Kilborn
                                        last edited by Ekopalypse

                                        @Alan-Kilborn said in Little Dialog-wrapper for PythonScript:

                                        I must be doing something wrong in a really basic sense?

                                        The only thing you did wrong was to assume that once the author changed the public API interface, he would also change the examples in the documentation, but … man … he screwed up :-)

                                        on_click != onClick
                                        
                                        Alan KilbornA 2 Replies Last reply Reply Quote 3
                                        • Alan KilbornA
                                          Alan Kilborn @Ekopalypse
                                          last edited by Alan Kilborn

                                          @Ekopalypse said in Little Dialog-wrapper for PythonScript:

                                          on_click != onClick

                                          It works better that way. :-)

                                          Probably the code in test_different_ways_to_create_dialogs.py in the __test__ folder needs the same change?


                                          One more oddity: With the aformentioned test_button.py script, I have to press the Close Dialog button TWICE before the script ends.

                                          It seems that the dialog IS closing with the first press, but something is reopening it?

                                          EkopalypseE 1 Reply Last reply Reply Quote 0
                                          • Alan KilbornA
                                            Alan Kilborn @rdipardo
                                            last edited by Alan Kilborn

                                            @rdipardo

                                            Your changes did indeed allow the dictionary-lookup script to run for me. Thanks!

                                            For others that might be interested, here’s a full version of Michael Vincent’s dictionary script with rdipardo’s changes:

                                            import json
                                            import urllib.request as requests
                                            import urllib.parse
                                            from Npp import editor
                                            
                                            from WinDialog import Button, DefaultButton, Dialog, Label, ListBox, TextBox
                                            from WinDialog.win_helper import WindowStyle as WS
                                            
                                            TITLE = "Dictionary"
                                            
                                            class Returns(object):
                                                """The input / output for the Dictionary service."""
                                                def __init__(self, word=""):
                                                    self.word = word
                                                    self.definition = ""
                                                    self.synonyms = []
                                                    self.antonyms = []
                                                    self.replace = None
                                            
                                            class Dictionary(Dialog):
                                                """A Dictionary dialog interface."""
                                                def __init__(self, ret=Returns()):
                                                    super().__init__(                title=TITLE      , center = True      , size=(220, 250))
                                                    self.word       = TextBox(                          position=(10, 12)  , size=(150, 14) )
                                                    self.lookup     = DefaultButton( title='&Lookup'  , position=(165, 13) , size=(45, 11)  )
                                                    self.definition = TextBox(                          position=(10, 30)  , size=(200, 100))
                                                    self.label1     = Label(         title='Synonyms' , position=(10, 140) , size=(45, 11)  )
                                                    self.synonyms   = ListBox(                          position=(10, 155) , size=(90, 65)  )
                                                    self.replsyn    = Button(        title='Re&place' , position=(10, 220),  size=(45, 11)  )
                                                    self.label2     = Label(         title='Antonyms' , position=(120, 140), size=(45, 11)  )
                                                    self.antonyms   = ListBox(                          position=(120, 155), size=(90, 65)  )
                                                    self.replant    = Button(        title='Repl&ace' , position=(120, 220), size=(45, 11)  )
                                                    self.close      = Button(        title='&Close'   , position=(165, 235), size=(45, 11)  )
                                            
                                                    self.ret = ret
                                            
                                                    self.onIdOk          = self.on_lookup
                                                    self.lookup.onClick  = self.on_lookup
                                                    self.replsyn.onClick = self.on_replace_syn
                                                    self.replant.onClick = self.on_replace_ant
                                                    self.close.onClick   = self.on_close
                                            
                                                    self.definition.style = self.definition.style | WS.VSCROLL | WS.HSCROLL # | WS.DISABLED
                                                    self.synonyms.style   = self.synonyms.style   | WS.TABSTOP
                                                    self.antonyms.style   = self.antonyms.style   | WS.TABSTOP
                                            
                                                    self.show()
                                            
                                                def _initialize(self):
                                                    self.ret = Returns(self.ret.word)
                                            
                                                    self.word.setText(self.ret.word)
                                                    self.synonyms.clear()
                                                    self.antonyms.clear()
                                            
                                                def initialize(self):
                                                    """Initialize the dialog."""
                                                    self._on_lookup()
                                            
                                                def _on_lookup(self):
                                                    """Lookup the word."""
                                                    self._initialize()
                                            
                                                    text_encoded = urllib.parse.quote(self.word.getText())
                                                    r = requests.urlopen(requests.Request(f"http://api.dictionaryapi.dev/api/v2/entries/en/{text_encoded}"))
                                                    if r.status != 200:
                                                        r.close()
                                                        return
                                            
                                                    synonyms = []
                                                    antonyms = []
                                                    response = ""
                                                    for idx, defs in enumerate(json.loads(r.read().decode('utf8'))[0]['meanings']):
                                                        response += f"{idx+1} : {defs['partOfSpeech']}\r\n"
                                                        for pos in defs['definitions']:
                                                            response += f"    {pos['definition']}\r\n"
                                                            synonyms.extend(pos['synonyms'])
                                                            antonyms.extend(pos['antonyms'])
                                                        synonyms.extend(defs['synonyms'])
                                                        antonyms.extend(defs['antonyms'])
                                            
                                                    self.ret.definition = response
                                                    self.definition.setText(self.ret.definition)
                                            
                                                    # Need case insensitive since ListBox has style SORT, which is case insensitive
                                                    self.ret.synonyms = sorted(set(synonyms), key=str.casefold)
                                                    self.ret.antonyms = sorted(set(antonyms), key=str.casefold)
                                                    self.synonyms.addStrings(self.ret.synonyms)
                                                    self.antonyms.addStrings(self.ret.antonyms)
                                                    r.close()
                                            
                                                def on_lookup(self):
                                                    self.ret.word = self.word.getText()
                                                    self._on_lookup()
                                            
                                                def on_replace_syn(self):
                                                    item = self.synonyms.getSelectedItem()
                                                    if item < 0:
                                                        return
                                            
                                                    self.ret.replace = self.synonyms._ListBox__items[item].value
                                                    editor.replaceSel(self.ret.replace)
                                                    self.terminate()
                                            
                                                def on_replace_ant(self):
                                                    item = self.antonyms.getSelectedItem()
                                                    if item < 0:
                                                        return
                                            
                                                    self.ret.replace = self.antonyms._ListBox__items[item].value
                                                    editor.replaceSel(self.ret.replace)
                                                    self.terminate()
                                            
                                                def on_close(self):
                                                    """Close dialog."""
                                                    self.terminate()
                                            
                                            def editor_getWordAtCaretOrSelection():
                                                retval = ''
                                                (sel_start, sel_end) = (editor.getSelectionStart(), editor.getSelectionEnd())
                                                if editor.getSelections() == 1 and sel_start != sel_end:
                                                    retval = editor.getTextRange(sel_start, sel_end)
                                                else:
                                                    start_of_word_pos = editor.wordStartPosition(editor.getCurrentPos(), True)
                                                    end_of_word_pos = editor.wordEndPosition(start_of_word_pos, True)
                                                    if start_of_word_pos != end_of_word_pos:
                                                        retval = editor.getTextRange(start_of_word_pos, end_of_word_pos)
                                                        editor.setSelection(end_of_word_pos, start_of_word_pos)
                                                return retval
                                            
                                            def lookup():
                                                word = ""
                                                if editor.getSelectionEmpty():
                                                    word = editor_getWordAtCaretOrSelection()
                                                else:
                                                    word = editor.getSelText()
                                            
                                                if len(word) <= 0:
                                                    notepad.messageBox('Select a word (or put the caret in a word) before running.', 'Error')
                                                    return
                                            
                                                ret = Returns(word)
                                                Dictionary(ret)
                                                # print(ret.word)
                                                # print(ret.definition)
                                                # print(ret.synonyms)
                                                # print(ret.antonyms)
                                                # print(ret.replace)
                                            
                                            if __name__ == '__main__':
                                                lookup()
                                            
                                            Michael VincentM 1 Reply Last reply Reply Quote 2
                                            • First post
                                              Last post
                                            The Community of users of the Notepad++ text editor.
                                            Powered by NodeBB | Contributors