• Login
Community
  • Login

Little Dialog-wrapper for PythonScript

Scheduled Pinned Locked Moved Notepad++ & Plugin Development
74 Posts 8 Posters 32.8k 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.
  • M
    Michael Vincent @Michael Vincent
    last edited by Aug 22, 2023, 12:12 PM

    @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
    • A
      Alan Kilborn @Michael Vincent
      last edited by Alan Kilborn Oct 8, 2023, 12:23 PM Oct 8, 2023, 12:11 PM

      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…

      R E 2 Replies Last reply Oct 9, 2023, 2:25 AM Reply Quote 2
      • R
        rdipardo @Alan Kilborn
        last edited by Oct 9, 2023, 2:25 AM

        @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()
        
        A 1 Reply Last reply Oct 9, 2023, 11:27 AM Reply Quote 1
        • E
          Ekopalypse @Alan Kilborn
          last edited by Ekopalypse Oct 9, 2023, 7:40 AM Oct 9, 2023, 7:39 AM

          @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
          
          A 2 Replies Last reply Oct 9, 2023, 11:20 AM Reply Quote 3
          • A
            Alan Kilborn @Ekopalypse
            last edited by Alan Kilborn Oct 9, 2023, 12:09 PM Oct 9, 2023, 11:20 AM

            @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?

            E 1 Reply Last reply Oct 10, 2023, 8:20 AM Reply Quote 0
            • A
              Alan Kilborn @rdipardo
              last edited by Alan Kilborn Oct 9, 2023, 11:31 AM Oct 9, 2023, 11:27 AM

              @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()
              
              M 1 Reply Last reply Oct 9, 2023, 1:45 PM Reply Quote 2
              • A
                Alan Kilborn @Ekopalypse
                last edited by Oct 9, 2023, 12:05 PM

                @Ekopalypse

                Something is definitely up with having to close things twice.

                When trying out various scripts from the __test__ folder, e.g. test_progressbar.py, test_statusbar.py, I have to press the X in the upper right of the window TWICE before the script truly ends.

                E 1 Reply Last reply Oct 9, 2023, 1:19 PM Reply Quote 1
                • E
                  Ekopalypse @Alan Kilborn
                  last edited by Oct 9, 2023, 1:19 PM

                  @Alan-Kilborn

                  Yeah, lol, apparently my code isn’t really self-explanatory, too bad, was hoping it was.
                  Each test_{control} script executes once the dialog by rc-generation and once as a class example.

                  A 1 Reply Last reply Oct 9, 2023, 1:34 PM Reply Quote 4
                  • A
                    Alan Kilborn @Ekopalypse
                    last edited by Alan Kilborn Oct 9, 2023, 1:38 PM Oct 9, 2023, 1:34 PM

                    @Ekopalypse said in Little Dialog-wrapper for PythonScript:

                    Each test_{control} script executes once the dialog by rc-generation and once as a class example.

                    Ha, okay, I wasn’t really digging into any of the code (yet!), just running things like a dumb user (success!) and misunderstanding what results were obtained.

                    Thanks for the clarification.

                    What I like to do if I have code that runs two ways is to let it only one way run via a controlling variable at the top. That way someone trying it can change one line (to change the variable) and the rest will run differently.

                    1 Reply Last reply Reply Quote 1
                    • M
                      Michael Vincent @Alan Kilborn
                      last edited by Oct 9, 2023, 1:45 PM

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

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

                      @rdipardo

                      Sorry I haven’t been around in a while, just getting back now. Thank you for the updates; I’ve converted my dictionary and translator scripts to use the more portable method as urllib does indeed come with PythonScript. And yes, I have the PythonScript plugin set to “see” my installed Python3 - which had the requests module installed.

                      Happy we’re getting some traction out of @Ekopalypse 's Dialog-wrapper - truly ingenious and adds lots of menu / dialog functionality to PythonScripts.

                      Cheers.

                      A 1 Reply Last reply Oct 10, 2023, 11:28 AM Reply Quote 3
                      • E
                        Ekopalypse @Alan Kilborn
                        last edited by Oct 10, 2023, 8:20 AM

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

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

                        Yes, correct and done, thanks for pointing it out.

                        What I like to do if I have code that runs two ways is …

                        They are tests, actually not meant as examples, my point was rather,
                        that the dialogs generated in each case, or more precisely, the resulting ByteArrays are compared,
                        but I understand what you mean. If I have a little more time sometime in the future,
                        I will tackle that.

                        Right now I’m using my meager free time to get the NppLspClient plugin into beta status, and then I wanted to rewrite the NppDebugger because my employer won’t allow me to release the existing plugin.
                        (The downside, if you develop something during working hours, suddenly it doesn’t belong to you anymore) :-(
                        (The advantage is that I can learn and use a new language, probably Rust or Zig, but I don’t know for sure yet) :-)

                        A 1 Reply Last reply Oct 10, 2023, 10:54 AM Reply Quote 2
                        • A
                          Alan Kilborn @Ekopalypse
                          last edited by Alan Kilborn Oct 10, 2023, 1:15 PM Oct 10, 2023, 10:54 AM

                          @Ekopalypse said in Little Dialog-wrapper for PythonScript:

                          They are tests, actually not meant as examples,

                          Without a specific set of examples, the tests become the examples. :-)

                          Actually, I think Michael led me down the road of the tests being examples…I didn’t think too hard about it at first.


                          my employer won’t allow me to release the existing plugin.
                          (The downside, if you develop something during working hours, suddenly it doesn’t belong to you anymore

                          It is reasonable that it doesn’t belong to you, but it seems unreasonable that your employer won’t release it. It would be understandable if your employer was a developer of rocket fuel and your plugin calculated optimized rocket fuel ingredient proportions… But as a general purpose tool that won’t give a competitor some sort of edge, why not share it? Sigh.

                          1 Reply Last reply Reply Quote 2
                          • A
                            Alan Kilborn @Michael Vincent
                            last edited by Oct 10, 2023, 11:28 AM

                            @Michael-Vincent

                            Not to get too far off-track with this thread, as it’s meant to highlight the Little Dialog-wrapper, but I noticed that in your “dictionary” script, if the selected “word” to look up isn’t spelled correctly (such that it isn’t a real word), unhandled exceptions occur (just check the PS console window).

                            Before I noticed this I got to thinking about the utility of dictionary programs. They’re fine if you know how the word is spelled and you want other info about it. But if you don’t know the spelling, it is hard to get you where you need to go.

                            Perhaps the script, if it doesn’t get an exact match, it should do a web search on the word of questionable spelling, e.g. N++'s Edit > On selection > Search on Internet. From that output, the user could correct the spelling input to the script and get further info about the word.

                            M 1 Reply Last reply Oct 10, 2023, 11:42 AM Reply Quote 1
                            • M
                              Michael Vincent @Alan Kilborn
                              last edited by Oct 10, 2023, 11:42 AM

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

                              selected “word” to look up isn’t spelled correctly (such that it isn’t a real word), unhandled exceptions occur (just check the PS console window).

                              Indeed. Quick fix:

                                  def _on_lookup(self):
                                      """Lookup the word."""
                                      self._initialize()
                              
                                      text_encoded = urllib.parse.quote(self.word.getText())
                                      headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36"}
                                      try:
                                          r = requests.urlopen(requests.Request(url=f"http://api.dictionaryapi.dev/api/v2/entries/en/{text_encoded}", headers=headers))
                                      except urllib.error.HTTPError:
                                          notepad.messageBox('No definitions found.  Perhaps misspelled?', 'Error')
                                          self.terminate()
                                          return
                                      if r.status != 200:
                                          self.terminate()
                                          return
                              

                              Cheers.

                              A 1 Reply Last reply Oct 10, 2023, 8:20 PM Reply Quote 1
                              • A
                                Alan Kilborn @Michael Vincent
                                last edited by Alan Kilborn Oct 10, 2023, 8:36 PM Oct 10, 2023, 8:20 PM

                                So I was attempting to do something “real” with the Little Dialog Wrapper, and it fell a bit short in capability in a couple of areas.

                                I’m not complaining, mind you, I just thought I’d share my findings. And, of course, I could be doing something wrong that is indeed quite simple to do correctly.


                                I wanted my dialog to start up with a checkbox in the checked state and I found no easy way to do it. I tried the obvious:

                                self.my_checkbox.setCheckState(BST.CHECKED)

                                right before the call to self.show(), but this did not achieve the goal.

                                The workaround that I came up with that did work was:

                                import threading
                                threading.Timer(0.25, lambda : self.my_checkbox.setCheckState(BST.CHECKED)).start()


                                I also found no obvious function (meaning part of the WinDialog hierarchy) to call to disable a control. With a little ctypes help I worked around by doing:

                                from ctypes import WinDLL
                                user32 = WinDLL('user32')
                                user32.EnableWindow(self.my_checkbox.hwnd, False)

                                And again, this didn’t work if I wanted to start up with a control disabled. So I resorted to:

                                threading.Timer(0.25, lambda : user32.EnableWindow(self.my_checkbox.hwnd, False)).start()

                                for that.

                                M E 3 Replies Last reply Oct 11, 2023, 12:06 AM Reply Quote 0
                                • M
                                  Michael Vincent @Alan Kilborn
                                  last edited by Oct 11, 2023, 12:06 AM

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

                                  I wanted my dialog to start up with a checkbox in the checked state and I found no easy way to do it. I tried the obvious:

                                  The way I got it to work was:

                                  class Returns(object):
                                      def __init__(self, U=None, I=False, X=False):
                                          self.user_input = U
                                          self.IGNORECASE = I
                                          self.REGEX      = X
                                          self._RESET      = False
                                          self._OK         = False
                                  
                                  class FilerLinesEditDlg(Dialog):
                                      def __init__(self, ret=Returns()):
                                          super().__init__(            title=TITLE                 , center = True,      size=(250, 75) )
                                          self.ok     = DefaultButton( title='&OK'                 , position=(135, 55), size=(50, 11)  )
                                          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 E&xpression' , position=(145, 30), size=(80, 14)  )
                                          self.cancel = Button(        title='&Cancel'             , position=(190, 55), size=(50, 11)  )
                                          self.reset  = Button(        title='&Reset'              , position=(45, 55),  size=(50, 11)  )
                                  
                                          self.ret = ret
                                  
                                          self.onIdOk         = self.on_ok
                                          self.ok.onClick     = self.on_ok
                                          self.cancel.onClick = self.on_cancel
                                          self.reset.onClick  = self.on_reset
                                          self.case.onClick   = self.on_case
                                          self.regex.onClick  = self.on_regex
                                  
                                          self.show()
                                  
                                      def initialize(self):
                                          self.edit.setText(self.ret.user_input)
                                          SendMessage(self.case.hwnd,  BM.SETCHECK, self.ret.IGNORECASE, 0)
                                          SendMessage(self.regex.hwnd, BM.SETCHECK, self.ret.REGEX, 0)
                                  

                                  This is the “relevant” parts of a larger script - my version of your filter lines editing actually.

                                  I create an object to store the values so then I can return them and save them in the global PythonScript object so next time I call it I can set the checkboxes appropriately. The initialize() function does the checkbox-ing.

                                  Cheers.

                                  A 1 Reply Last reply Oct 11, 2023, 11:45 AM Reply Quote 3
                                  • E
                                    Ekopalypse @Alan Kilborn
                                    last edited by Oct 11, 2023, 7:22 AM

                                    @Alan-Kilborn

                                    @Michael-Vincent is correct, to quote from the Dialog help

                                     |  initialize(self)
                                     |      Initializes the dialog and its controls at runtime.
                                     |      
                                     |      This method is intended to be overridden by a concrete class.
                                     |      It is executed after all controls have been created but before the dialog is displayed.
                                     |      Concrete implementations should provide custom logic to set up initial values, states, and configurations of the controls.
                                    

                                    It might be worth adding function(s) to enable or disable controls, yes. PRs are welcome :-)

                                    1 Reply Last reply Reply Quote 3
                                    • E
                                      Ekopalypse @Alan Kilborn
                                      last edited by Oct 11, 2023, 11:29 AM

                                      @Alan-Kilborn

                                      simple example

                                      from WinDialog import Dialog, CheckBoxButton
                                      from WinDialog.controls.button import BST
                                      
                                      class Example(Dialog):
                                          def __init__(self):
                                              super().__init__(size=(100, 100))
                                              self.btn1 = CheckBoxButton(title='Click me', position=(35, 40), size=(50, 14))
                                              self.show()
                                      
                                          def initialize(self):
                                              self.btn1.setCheckState(BST.CHECKED)
                                              
                                      Example()
                                      

                                      b6fa9e07-ccb0-4d72-983f-97b60eff24b3-image.png

                                      1 Reply Last reply Reply Quote 2
                                      • A
                                        Alan Kilborn @Michael Vincent
                                        last edited by Oct 11, 2023, 11:45 AM

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

                                        I create an object to store the values so then I can return them and save them in the global PythonScript object so next time I call it I can set the checkboxes appropriately.

                                        This is interesting. How do you kick off the saving of the current values? Meaning, if user presses Esc to close the dialog, or presses the red X in the title bar of the dialog, are you able to capture this and save your current control values? Maybe in your script you wouldn’t want to save in these circumstances, but OTOH maybe you would…

                                        E 1 Reply Last reply Oct 11, 2023, 12:02 PM Reply Quote 0
                                        • E
                                          Ekopalypse @Alan Kilborn
                                          last edited by Oct 11, 2023, 12:02 PM

                                          @Alan-Kilborn

                                          You have an initial state when you start the dialog and from there I would use the control events to get the values you need. Something like

                                          from WinDialog import Dialog, CheckBoxButton
                                          from WinDialog.controls.button import BST
                                          
                                          class Example(Dialog):
                                              def __init__(self):
                                                  super().__init__(size=(100, 100))
                                                  self.counter = 0
                                                  self.btn1 = CheckBoxButton(title='Click me', position=(35, 40), size=(50, 14))
                                                  self.btn1.onClick = self.on_click
                                                  self.show()
                                          
                                              def initialize(self):
                                                  self.btn1.setCheckState(BST.CHECKED)
                                                  
                                              def on_click(self):
                                                  self.counter += 1
                                                  
                                          e = example()
                                          print(e.counter)
                                          
                                          A 2 Replies Last reply Oct 11, 2023, 12:16 PM Reply Quote 1
                                          • First post
                                            Last post
                                          The Community of users of the Notepad++ text editor.
                                          Powered by NodeBB | Contributors