• Login
Community
  • Login

Little Dialog-wrapper for PythonScript

Scheduled Pinned Locked Moved Notepad++ & Plugin Development
74 Posts 8 Posters 13.3k 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 @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
            • A
              Alan Kilborn @Ekopalypse
              last edited by Alan Kilborn Oct 11, 2023, 12:18 PM Oct 11, 2023, 12:16 PM

              @Ekopalypse said in Little Dialog-wrapper for PythonScript:

              … use the control events to get the values you need.

              Sure, but that’s the obvious case.

              That’s why I specifically asked MV “…if user presses Esc to close the dialog, or presses the red X in the title bar of the dialog…”

              As far as I can tell, and yes I’ve read some of the docs now :-), these aren’t capturable events?

              M 1 Reply Last reply Oct 11, 2023, 1:30 PM Reply Quote 0
              • A
                Alan Kilborn @Ekopalypse
                last edited by Alan Kilborn Oct 11, 2023, 12:35 PM Oct 11, 2023, 12:31 PM

                @Ekopalypse

                BTW, I sort of missed this at first from the github page:

                help(WinDialog)

                I guess I’m just not used to documentation being provided in only this way, at least not any more. It’s totally fine, though, just maybe not super obvious for a newbie (would there be any newbies wanting to experiment with LDW??).

                So maybe I’ll offer this on the off-chance that a newbie is reading; this is what I did to make it easier to refer to (note: nothing rocket sciencey here…):

                • go to PythonScript console window area, right-click, and choose Clear from the popup menu
                • at the >>> prompt, type import WinDialog and press return
                • type help(WinDialog) and press return
                • right-click again and choose Select All and then press Ctrl+c
                • create a new tab in Notepad++ and paste there
                • save the tab for later reference
                1 Reply Last reply Reply Quote 2
                • M
                  Michael Vincent @Alan Kilborn
                  last edited by Oct 11, 2023, 1:30 PM

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

                  That’s why I specifically asked MV “…if user presses Esc to close the dialog, or presses the red X in the title bar of the dialog…”

                  They get saved. That’s why I use a return class object that’s part of the main script. So in the example I provided, I want to remember the setting for “ignore case” and “regex” so the checkboxes have an onClick event that toggles the boolean value stored in the return class.

                      def on_case(self):
                          self.ret.IGNORECASE = not self.ret.IGNORECASE
                  
                      def on_regex(self):
                          self.ret.REGEX = not self.ret.REGEX
                  

                  That happens whenever the users clicks the checkbox. So if they later just press ‘Esc’ or red ‘X’, the value has already been stored and then next time it launches, it is checked (or unchecked) appropriately in the initialize().

                  Cheers.

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

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

                    They get saved.

                    It’s not what I was truly getting at, but I understand how you’ve made it work. Thanks.

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

                      @Alan-Kilborn

                      I don’t seem to understand your use case.

                      If I want to know if a user either pressed ESC or clicked close_X to simply exit the dialog without anything being triggered by the dialog, then I would start the dialog with a variable has_been_cancelled=True and override it with the click event of the “normal” exit_dialog_button.

                      If that’s not what you’re looking for, maybe you can specify a simple scenario of what should be handled?

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

                        @Ekopalypse

                        I meant an optional user (the programmer-user) function that gets called when the dialog is closing, by whatever means. Then there’s a chance to do things at that point, much like, but the opposite of initialize(). Maybe I will think about working on that kind of thing, along with enabling/disabling of controls.

                        1 Reply Last reply Reply Quote 1
                        • A Alan Kilborn referenced this topic on Oct 13, 2023, 11:17 AM
                        • A
                          Alan Kilborn
                          last edited by Alan Kilborn Oct 13, 2023, 11:48 AM Oct 13, 2023, 11:19 AM

                          My very first script using this wrapper is HERE !!

                          I think it turned out pretty well and puts this wrapper through its paces. :-)

                          About the only annoying thing is that the dialog is modal, i.e., one can’t switch between it and the Notepad++ editor window. This is annoying because one can do this with Notepad++'s Find window, and thus one gets used to this behavior.

                          Maybe another suggested extension is that if the dialog (is made modeless) and loses input focus, the script ends and the dialog disappears. Sure, the user would have to restart the script (via assigned keycombo) after working in the editor window, but this isn’t too bad. The restarted script would pick up right where the closed one was (with all settings intact).

                          P 1 Reply Last reply Oct 13, 2023, 7:14 PM Reply Quote 2
                          • P
                            PeterJones @Alan Kilborn
                            last edited by PeterJones Oct 13, 2023, 7:19 PM Oct 13, 2023, 7:14 PM

                            @Alan-Kilborn said,

                            My very first script using this wrapper is HERE !!

                            And in chat, he asked me to give it a test drive.

                            From the user perspective, I was able to follow the instructions on @Ekopalypse’s GitHub page to get the WinDialog directory installed in my instance, and @Alan-Kilborn’s instructions to run his wrapper. So “yay” for sufficient “installation” instructions on both. :-)

                            About the only annoying thing is that the dialog is modal, i.e., one can’t switch between it and the Notepad++ editor window. This is annoying because one can do this with Notepad++'s Find window, and thus one gets used to this behavior.

                            Indeed. Even after having read this paragraph, and knowing that it was modal, I still tried to change my selection in the editor without closing the dialog. So yes, having a modeless option for dialogs would definitely help with the user interface.

                            Maybe another suggested extension is that if the dialog (is made modeless) and loses input focus, the script ends and the dialog disappears. Sure, the user would have to restart the script (via assigned keycombo) after working in the editor window, but this isn’t too bad. The restarted script would pick up right where the closed one was (with all settings intact).

                            Ah, I assume this was the use-case for you wanting to capture the ESC / X events <update>-- whoops, left that sentence hanging, without explaining that I meant that I thought you wanted it to be able to save settings, without having an OK button. I might recommend seeing if you can get on-changed events for the individual settings controls, and have them save their settings, whether or not OK (or, in your case, Count) is eventually pressed</update>

                            (And sorry, Alan, I know you were encouraging me to comment on the LittleDialogWrapper aspects as well – presumably from a developer perspective – to see if I could put it through more paces. But until I switch over to PS3 whenever it’s out of alpha, I’m not likely to incorporate LittleDialogWrapper into any of my scripts, because my PS installation is going to stay at whichever can be easily found in Plugins Admin, so that when I’m trying to help users with PS solutions, they won’t have an extra step of installing the alpha version of the plugin manually, or so I don’t have to go switch to a different instance of Notepad++ compared to my normal workflow. So all I’ve got for now is what I said in April, and my brief user-perspective comments in this post.)

                            A 1 Reply Last reply Oct 14, 2023, 11:36 AM Reply Quote 1
                            • A
                              Alan Kilborn @PeterJones
                              last edited by Alan Kilborn Oct 14, 2023, 11:37 AM Oct 14, 2023, 11:36 AM

                              @PeterJones

                              I assume this was the use-case for you wanting to capture the ESC / X events

                              My points about “modeless” and “easier saving of settings” were totally separate.

                              I wasn’t really saying that the dialog should truly be “modeless”, because if it were, users would attempt to run other scripts after refocusing the editing window (leaving the dialog open), and PythonScript, presumably for good reasons, disallows running a second script while a first is running.

                              So my thinking was the dialog script could end when focus is lost, to be restored when next invoked. (Ah, okay, I see the relationship of “saving the settings” – but truly, this was an earlier, separate point).


                              I might recommend seeing if you can get on-changed events for the individual settings controls, and have them save their settings

                              Sure. Again, my earlier point was that this is “drudge work”. I have to have a “click” event on every control, even one that normally doesn’t need one, just for saving its state?? How about just a single function that gets called, whenever the dialog is about to close, by any means, that allows me to do “cleanup”, be it saving settings or something else?


                              But until I switch over to PS3 …

                              I decided to make the switch recently, although I still have to sort out some things that don’t work in scripts I had totally working under PS2.

                              In general, I think it is time to move forward on this. PS3 may never come out of “alpha”, because what keeps it there (lack of old encoding support) doesn’t seem to get worked on.

                              It seems reasonable that the PS developers make v. 3.x the one that Plugins Admin installs, but keep 2.x around for those that use scripting with dated encodings. Or heck, even make BOTH PS3 and PS2 installable via Plugins Admin.

                              1 Reply Last reply Reply Quote 2
                              • A
                                Alan Kilborn
                                last edited by Alan Kilborn Oct 14, 2023, 12:25 PM Oct 14, 2023, 12:23 PM

                                Preview of the PR for enable/disable of controls:

                                In __control_template.py, at the bottom, add two member functions to the Control class:

                                def enable(self, e : bool) -> bool: return True if WinDLL('user32').EnableWindow(self.hwnd, e) else False
                                
                                def isEnabled(self) -> bool: return True if WinDLL('user32').IsWindowEnabled(self.hwnd) else False
                                

                                And of course, near the top of the file:

                                from ctypes import WinDLL
                                
                                1 Reply Last reply Reply Quote 1
                                • A Alan Kilborn referenced this topic on Nov 10, 2023, 2:10 PM
                                • S
                                  SN-CH
                                  last edited by Mar 6, 2024, 12:48 PM

                                  @Ekopalypse, @Alan-Kilborn

                                  Good afternoon,

                                  I would appreciate if you could give me a word of advice.

                                  I wonder if solution of this thread can help me to solve the following task: I need to pass integer argument to the Python script I run in N++ with PythonScript. Does it make sense to use this wrapper or is it an overkill because there is an easier way to do this?

                                  A 1 Reply Last reply Mar 6, 2024, 1:17 PM Reply Quote 1
                                  • A
                                    Alan Kilborn @SN-CH
                                    last edited by Alan Kilborn Mar 6, 2024, 1:33 PM Mar 6, 2024, 1:17 PM

                                    @SN-CH

                                    I need to pass integer argument to the Python script I run… Does it make sense to use this wrapper or is it … overkill because there is an easier way…?

                                    It sounds like it would be overkill, but only you can judge that.

                                    About the simplest way to achieve what you are asking for is:

                                    z = notepad.prompt('prompt text:', 'title bar of window', '')
                                    if z is not None:
                                        z = int(z)
                                        print(z)
                                    

                                    which will give you a (no frills) window to enter your integer into:

                                    9143568b-5e65-4ebf-aac6-2d5e3a9007ac-image.png

                                    1 Reply Last reply Reply Quote 4
                                    • S
                                      SN-CH
                                      last edited by Mar 6, 2024, 8:43 PM

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

                                      z = notepad.prompt(‘prompt text:’, ‘title bar of window’, ‘’)

                                      Works perfectly. Much appreciated.

                                      1 Reply Last reply Reply Quote 2
                                      • M Michael Vincent referenced this topic on Mar 29, 2024, 3:57 PM
                                      • P
                                        PeterJones @Ekopalypse
                                        last edited by Mar 29, 2024, 5:40 PM

                                        @Ekopalypse ,

                                        After reading this post, I wanted to try out @Michael-Vincent’s translation script.

                                        So I went to my portable that has PythonScript 3.0.17 installed, and tried to install based on the README instructions and tried to run the first example there. I get

                                        Python 3.12.1 (tags/v3.12.1:2305ca5, Dec  7 2023, 22:03:25) [MSC v.1937 64 bit (AMD64)]
                                        Initialisation took 406ms
                                        Ready.
                                        Traceback (most recent call last):
                                          File "C:\usr\local\apps\npp\npp862.ps3\plugins\Config\PythonScript\scripts\windlg_ex1.py", line 1, in <module>
                                            from WinDialog import Dialog, Button, create_dialog_from_rc
                                        ModuleNotFoundError: No module named 'WinDialog'
                                        

                                        In following the instructions, I put the WinDialog folder in the user directory ...plugins\Config\PythonScript\lib, as instructed. I can verify it’s there:

                                        > cd plugins
                                        > dir
                                         Directory of C:\usr\local\apps\npp\npp862.ps3\plugins
                                        
                                        02/08/2024  01:28 PM    <DIR>          .
                                        02/08/2024  01:28 PM    <DIR>          ..
                                        02/08/2024  01:29 PM    <DIR>          ColumnTools
                                        03/29/2024  09:36 AM    <DIR>          Config
                                        01/14/2024  03:33 AM    <DIR>          doc
                                        02/08/2024  01:09 PM    <DIR>          mimeTools
                                        02/08/2024  01:09 PM    <DIR>          NppConverter
                                        02/08/2024  01:09 PM    <DIR>          NppExport
                                        02/08/2024  01:12 PM    <DIR>          PythonScript
                                        
                                        > dir Config
                                         Directory of C:\usr\local\apps\npp\npp862.ps3\plugins\Config
                                        
                                        03/29/2024  09:36 AM    <DIR>          .
                                        03/29/2024  09:36 AM    <DIR>          ..
                                        03/29/2024  10:27 AM               179 ColumnTools.ini
                                        02/08/2024  01:12 PM               646 converter.ini
                                        02/08/2024  01:07 PM           208,728 nppPluginList.dll
                                        03/29/2024  10:23 AM    <DIR>          PythonScript
                                        
                                        > dir Config\PythonScript
                                         Directory of C:\usr\local\apps\npp\npp862.ps3\plugins\Config\PythonScript
                                        
                                        03/29/2024  10:23 AM    <DIR>          .
                                        03/29/2024  10:23 AM    <DIR>          ..
                                        03/29/2024  10:08 AM    <DIR>          lib
                                        03/29/2024  10:25 AM    <DIR>          scripts
                                        
                                        > dir Config\PythonScript\lib\WinDialog
                                         Directory of C:\usr\local\apps\npp\npp862.ps3\plugins\Config\PythonScript\lib\WinDialog
                                        
                                        03/29/2024  10:08 AM    <DIR>          .
                                        03/29/2024  10:08 AM    <DIR>          ..
                                        03/29/2024  10:08 AM    <DIR>          controls
                                        03/29/2024  10:08 AM             2,748 readme.md
                                        03/29/2024  10:08 AM    <DIR>          resource_parser
                                        03/29/2024  10:08 AM         2,145,865 test_dynamic_dialog2.gif
                                        03/29/2024  10:08 AM    <DIR>          win_helper
                                        03/29/2024  10:08 AM            22,893 __init__.py
                                        03/29/2024  10:08 AM    <DIR>          __tests__
                                        

                                        If I move WinDialog from the user directory to the machine directory (C:\usr\local\apps\npp\npp862.ps3\plugins\PythonScript\lib), then the script can see WinDialog and it loads and runs fine.

                                        Is there some magic that needs to go in the startup.py to make PS3 see the user-lib directory for a portable installation?

                                        Debug Info

                                        Notepad++ v8.6.2   (64-bit)
                                        Build time : Jan 14 2024 - 02:16:00
                                        Path : C:\usr\local\apps\npp\npp862.ps3\notepad++.exe
                                        Command Line : 
                                        Admin mode : OFF
                                        Local Conf mode : ON
                                        Cloud Config : OFF
                                        OS Name : Windows 10 Enterprise (64-bit)
                                        OS Version : 22H2
                                        OS Build : 19045.4170
                                        Current ANSI codepage : 1252
                                        Plugins : 
                                            ColumnTools (1.4.5.1)
                                            mimeTools (3)
                                            NppConverter (4.5)
                                            NppExport (0.4)
                                            PythonScript (3.0.17)
                                        
                                        M 2 Replies Last reply Mar 29, 2024, 5:58 PM Reply Quote 1
                                        • M
                                          Michael Vincent @PeterJones
                                          last edited by Mar 29, 2024, 5:58 PM

                                          @PeterJones said in Little Dialog-wrapper for PythonScript:

                                          PythonScript 3.0.17

                                          That’s “most likely” the problem. See here:

                                          https://github.com/bruderstein/PythonScript/issues/322

                                          I have stayed on 3.0.16 because of this and until this gets fixed.

                                          Cheers.

                                          P mpheathM 2 Replies Last reply Mar 29, 2024, 6:31 PM Reply Quote 4
                                          56 out of 74
                                          • First post
                                            56/74
                                            Last post
                                          The Community of users of the Notepad++ text editor.
                                          Powered by NodeBB | Contributors