theodox / mGui

Python module for cleaner maya GUI layout syntax
MIT License
123 stars 23 forks source link

Help about TreeView #42

Closed Digitalboy1977 closed 7 years ago

Digitalboy1977 commented 8 years ago

Hi, I have a problem with TreeView control. I want to connect a function to the TreeView buttons like you do with the normal Button.

My sample code:

This inside the main init function

treeViewTest = gui.TreeView('Viewer', numberOfButtons=2) treeViewTest .addItem = ('Work1', '') treeViewTest .pressCommand += self.buttonAction

This what button should call function

def buttonAction(self, _args, *_kwargs): template = kwargs['sender'] print template

ERROR MESSAGE: TypeError: Invalid arguments for flag 'pressCommand'. Expected ( int, string or function ), got MayaEvent

But if I do like this: treeViewTest .pressCommand =[(1,self.buttonAction),(2,self.buttonAction),(3,self.buttonAction)] It does work but I cant get the sender. Which button I clicked on etc... Only the arguments the controller sends.

This is the example when you connect with button controller and that works: buttonTest = gui.Button('Reset to Default') buttonTest .command += self.buttonAction

Probably I write something totally stupid wrong, so please teach me why this do not work. And how you would solve this situation.

Best regards, Mattias Eriksson.

theodox commented 8 years ago

It looks like Treeview breaks the usual signature for press commands. I'll take a look at it and see what the best workaround is, or if you want to try submit a pull request and we can try it out.

Sent from my iPhone

On Oct 12, 2016, at 5:29 AM, Digitalboy1977 notifications@github.com wrote:

Hi, I have a problem with TreeView control. I want to connect a function to the TreeView buttons like you do with the normal Button.

My sample code: ##############################################

This inside the main init function

treeViewTest = gui.TreeView('Viewer', numberOfButtons=2) treeViewTest .addItem = ('Work1', '') treeViewTest .pressCommand += self.buttonAction

This what button should call function

def buttonAction(self, args, *kwargs): template = kwargs['sender'] print template ############################################## ERROR MESSAGE: TypeError: Invalid arguments for flag 'pressCommand'. Expected ( int, string or function ), got MayaEvent

But if I do like this: treeViewTest .pressCommand =[(1,self.buttonAction),(2,self.buttonAction),(3,self.buttonAction)] It does work but I cant get the sender. Which button I clicked on etc... Only the arguments the controller sends.

This is the example when you connect with button controller and that works: buttonTest = gui.Button('Reset to Default') buttonTest .command += self.buttonAction

Probably I write something totally stupid wrong, so please teach me why this do not work. And how you would solve this situation.

Best regards, Mattias Eriksson.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

theodox commented 8 years ago

This command - I'm not sure if any other maya widgets do this - expects an array of commands instead of a single callback, that's why it's breaking. We'll probably need to specialize the delegate just for this one, although maybe we could alias the command flag some other way. Suggestions welcome!

Digitalboy1977 commented 8 years ago

Thank you Theodox for a fast reply. I haven't read the source code so deep into it yet.

Any solution should be good I think. I will try to read the source code more this weekend, and see what ideas I get. Its difficult to make time for it on weekday when you live in Japan. ;)

Sorry for not be so helpful right now.

theodox commented 8 years ago

I added a new file, treeView.py, which includes a wrapper for the regular TreeView that's a bit easier to use. I think it could use a little more work, so please look it over and make suggestions. Treeview is a wierd command (it has a lot of set-only properties, which is unlike the rest of maya)

Anyway, if you get the remove_keys branch you can try this:

from mGui.treeView import MTreeView
from mGui.gui import *
from mGui.forms import *

with Window() as win:
    with FillForm() as form:
        tree = MTreeView(numberOfButtons = 2, width = 256, height = 256)

tree.set_items(**{'a':'b', 'b':'a', 'c':'a'})

def btn_a(*_,**kw):
    selected = kw['tree_view'].selectItem
    print 'items', selected, 'button', kw['button_index']

def btn_b(*_,**kw):
    print 'button 2 was pressed'

tree.buttons[0].pressed += btn_a
tree.buttons[1].pressed += btn_b

win.show()
theodox commented 8 years ago

After we've played with it a bit we can move it over into the main branch

Digitalboy1977 commented 8 years ago

Hi Theodox,

Great work! It almost work perfect, I played around with your code a bit.

The problem now is you must select an item to get the buttons parents name. That fails when you select item and then click on another button row, it gets wrong parent.

To make it simple to explain the situation: These two buttons will for example have "turn on" / "turn off" label action for its parent Item.

Change the (def btn_a) (def btn_b) function to these and try:

def btn_a(*_,**kw):
    selected = kw['tree_view'].selectItem
    print 'items', selected, 'button', kw['button_index']
    kw['tree_view'].enableLabel =(selected[0],0)

def btn_b(*_,**kw):
    selected = kw['tree_view'].selectItem
    print 'button 2 was pressed'
    kw['tree_view'].enableLabel =(selected[0],1)
theodox commented 8 years ago

I was just offering that as an example. TreeView is not one of my favorite controls - it's got a very wierd API compare to most of them -- so I haven't used it very much myself but my impression is that the treeView buttons will fire two arguments when they are clicked: the selection and the button state. try printing out the *args variable in the function and see if it is giving you the info you need:

def btn_a(*args,**kw):
    print 'button args', args
    print 'mGui context', kw

On Thu, Oct 13, 2016 at 1:29 AM, Digitalboy1977 notifications@github.com wrote:

Hi Theodox,

Great work! It almost work perfect, I played around with your code a bit.

The problem now is you must select an item to get the buttons parents name. That fails when you select item and then click on another button row, it gets wrong parent.

To make it simple to explain the situation: These two buttons will for example have "turn on" / "turn off" label action for its parent Item.

Change the (_def btna) (_def btnb) function to these and try:

def btn_a(_,*kw): selected = kw['tree_view'].selectItem print 'items', selected, 'button', kw['button_index'] kw['tree_view'].enableLabel =(selected[0],0)

def btn_b(_,*kw): selected = kw['tree_view'].selectItem print 'button 2 was pressed' kw['tree_view'].enableLabel =(selected[0],1)

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/theodox/mGui/issues/42#issuecomment-253448515, or mute the thread https://github.com/notifications/unsubscribe-auth/AD3mGJiKIYKFKblUEQN7_XNQXhfG_z5wks5qzevfgaJpZM4KUst1 .

Digitalboy1977 commented 8 years ago

Hi Theodox, Sorry for late answer. But I want to confirm that latest suggestion works perfect:

def btn_a(*args,**kw):
        print 'button args', args
        print 'mGui context', kw

With that I am able to get these arguments I want to have. Just one last question, What is difference between (args) and (_)? I know I have read about it somewhere when you explained it, but can you point me to right place so I can read it again. :)

Anyway, great help and great stuff you have done. Keep it up!

theodox commented 8 years ago

Actually you could use those interchangeably if you really wanted to... * is just a convention in Python for "I have an argument set here but I plan on ignoring it". When I switched over to actually using that argument set I just renamed the variable to something more ordinary. I could have kept the name "" and it would have been fine, just confusing to look at.

Sent from my iPhone

On Oct 16, 2016, at 8:18 PM, Digitalboy1977 notifications@github.com wrote:

Hi Theodox, Sorry for late answer. But I want to confirm that latest suggestion works perfect:

def btn_a(_args,__kw): print 'button args', args print 'mGui context', kw With that I am able to get these arguments I want to have. Just one last question, What is difference between (args) and (*)? I know I have read about it somewhere when you explained it, but can you point me to right place so I can read it again. :)

Anyway, great help and great stuff you have done. Keep it up!

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

theodox commented 8 years ago

If you're happy with this one I'll mark this issue fixed when I integrate 59bc1ddfe376169790ea422475aa27f973a18579 - should be a week or so. Thanks for the feedback.

theodox commented 7 years ago

Finally got this committed to master - it's in now