Need help with a feature in a simple plugin

Do you have questions about writing plugins or scripts in Python? Meet the coders here.
Post Reply
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Need help with a feature in a simple plugin

Post by krambriw » Wed Jun 01, 2011 10:21 am

Hello,

I have this simple plugin below. In the action configration settings there are two drop downs for selecting choices.

The first is named "Tag" and the second is named "Item"

The choices for "Tag" is a, b, c or d

Now I would like the following that when I select a choice for "Tag" that the list of choices for "Items" are updated accordingly.

Unfortunately this requires the click on the OK button first to happen and when I open configuration again, it is updated

But I wan't update directly on the choice made. Is this possible? Any hints very much appreciated

My best regards, Walter
__init__.py
(2.75 KiB) Downloaded 280 times

Code: Select all

version = "0.0.0"

eg.RegisterPlugin(
    name = "Test",
    author = "krambriw",
    version = version,
    kind = "program",
    description = "Testing",
)
import wx.lib

#===============================================================================
class Test(eg.PluginClass):
    
    def __init__(self):
        self.AddAction(MyAction)

class MyAction(eg.ActionClass):
    class text:
        txtMyValue = "My value"

        
    def __call__(self, tag, item):
        print tag
        print item
        
                
    # Get the choice from dropdown and perform some action
    def OnChoice(self, event):
        choice = event.GetSelection()
        event.Skip()
        return choice


    def Configure(
        self,
        tag = "",
        item = ""
        ):
            
        panel = eg.ConfigPanel(self)

        # Create a dropdown to select tag
        tagCtrl = wx.Choice(parent=panel, pos=(10,10)) 
        t_list = ['a','b','c','d']
        tagCtrl.AppendItems(strings=t_list) 
        if t_list.count(tag)==0:
            tagCtrl.Select(n=0)
        else:
            tagCtrl.SetSelection(int(t_list.index(tag)))
        tag = tagCtrl.GetStringSelection()
        
        staticBox = wx.StaticBox(panel, -1, "Select Tag")
        staticBoxSizer = wx.StaticBoxSizer(staticBox, wx.VERTICAL)
        sizer1 = wx.BoxSizer(wx.HORIZONTAL)
        sizer1.Add(tagCtrl, 1, wx.EXPAND)
        staticBoxSizer.Add(sizer1, 0, wx.EXPAND|wx.ALL, 5)
        panel.sizer.Add(staticBoxSizer, 0, wx.EXPAND)
        tagCtrl.Bind(wx.EVT_CHOICE, self.OnChoice)

        # Create a dropdown to select item
        itemCtrl = wx.Choice(parent=panel, pos=(10,10)) 

        if tag == "a":
            i_list = ['a','b','c','d']
        elif tag == "b":
            i_list = ['b','c','d','e']
        elif tag == "c":
            i_list = ['c','d','e','f']
        elif tag == "d":
            i_list = ['d','e','f','g']

        itemCtrl.AppendItems(strings=i_list) 
        if i_list.count(item)==0:
            itemCtrl.Select(n=0)
        else:
            itemCtrl.SetSelection(int(i_list.index(item)))
        item = itemCtrl.GetStringSelection()
        
        staticBox = wx.StaticBox(panel, -1, "Select Item")
        staticBoxSizer = wx.StaticBoxSizer(staticBox, wx.VERTICAL)
        sizer2 = wx.BoxSizer(wx.HORIZONTAL)
        sizer2.Add(itemCtrl, 1, wx.EXPAND)
        staticBoxSizer.Add(sizer2, 0, wx.EXPAND|wx.ALL, 5)
        panel.sizer.Add(staticBoxSizer, 0, wx.EXPAND)
        itemCtrl.Bind(wx.EVT_CHOICE, self.OnChoice)
     
        while panel.Affirmed():
            panel.SetResult(
                tagCtrl.GetStringSelection(),
                itemCtrl.GetStringSelection()
            )


User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Need help with a feature in a simple plugin

Post by Pako » Wed Jun 01, 2011 12:21 pm

Unfortunately I did not understand exactly how it should work.
So I made ​​two versions. Version two is a little easier
but I do not know whether it is possible to do it this way.

Pako
Attachments
__init__.py
Version 1
(3.57 KiB) Downloaded 282 times
__init__.py
Version 2
(3.56 KiB) Downloaded 284 times

krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: Need help with a feature in a simple plugin

Post by krambriw » Wed Jun 01, 2011 4:46 pm

Dear Pako, thank you very much for your help, both versions works fine!

Have a nice weekend, Walter

krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: Need help with a feature in a simple plugin

Post by krambriw » Thu Jun 02, 2011 7:32 pm

Dear Pako,
Just one more question (your "Test-version_1")

The "tag" selection is saved fine in configuration but not the "item" selection.

Do you know how to also make the "item" beeing saved?

Example:

- select "tag" "b"
(this makes the "item" list in drop-down to be refreshed correctly to start also with "b")
- now select "item" "c"
- click "OK"
- open again
- "item" should be "c" but is "b"


Best regards, Walter

User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Need help with a feature in a simple plugin

Post by Pako » Fri Jun 03, 2011 5:01 am

krambriw wrote:Dear Pako,
Just one more question (your "Test-version_1")
The "tag" selection is saved fine in configuration but not the "item" selection.
Do you know how to also make the "item" beeing saved?
Dear Walter,
Sure. It just falls into my sentence Unfortunately I did not understand exactly how it should work.
Modification is easy. Try the corrected version.
But I still not sure that it works according to your intentions.

Best regards, Pako
Attachments
__init__.py
Version 1
(3.69 KiB) Downloaded 282 times
__init__.py
Version 2
(3.68 KiB) Downloaded 283 times

krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: Need help with a feature in a simple plugin

Post by krambriw » Sat Jun 04, 2011 8:47 pm

Dear Pako,
Excellent advice, once again you showed how to do, I am very grateful!

I have now moved further with the test plugin for my final little try with this example

In the attached file (I focused on your test 1 only) I have added a third control, it is named "data".

The idea is the following

1) You select the tag, the list of items is then automatically populated depending on what the tag provides (various tags have different type of items). This is working excellent in your samples

Now this is new:

2) You select the item and depending on the selected items data type, the "data" field shall be changed automatically to allow input or selection of the corresponding data.

The item data type can be any type, bool, string, int, num, ......

The pictures below shows two cases, the first where data type is bool, the second where data type is string

The problem I have is that the "data" control is not updated on item selection event, I have to click ok and re-open, then it is correct shown.

I would like to update the "data" control correctly at once when I select item from the item drop down.

Is this possible?

I hope I have explained okay...

My very best regards, Walter
__init__.py
(5.41 KiB) Downloaded 268 times
Image1.jpg
Image2.jpg

User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Need help with a feature in a simple plugin

Post by Pako » Sun Jun 05, 2011 10:21 am

Dear Walter,
Attached is one possible solution.
Unfortunately I do not know if you'll need for some purposes the OnDataChoice function.
If so, it will require some further adjustments.

Best regards, Pako
Attachments
__init__.py
Enhanced Version 1
(5.77 KiB) Downloaded 280 times

krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: Need help with a feature in a simple plugin

Post by krambriw » Sun Jun 05, 2011 6:07 pm

Dear Pako, it works perfect!!!

I believe I do not need the OnDataChoice function, everything works so fine without it, maybe would be needed if we would continue to add even more ctrls :D

You have helped me with a very interesting subject, I wanted to have a dialog where:
- the selection from the first drop-down (tags) decides what shall be available for the user in the next drop-down (items)
- the selection of item gives the user possibility to enter or select proposed data of the correct type

Everything this works excellent, a big thank you!

I am going to use this technique in a plugin for OPC that I'm working on. I think this is also very useful for other plugin developers needing the same features

My Very Best Regards, Walter

Post Reply