CGTalk > Software > Autodesk Maya > Maya Programming
Login register
Thread Closed share thread « Previous Thread | Next Thread »  
 
Thread Tools Search this Thread Display Modes
Old 09-30-2012, 02:24 AM   #1
cwisbg
PRO
 
cwisbg's Avatar
portfolio
Brandon Gillespie
generalist
High Impact
Denver, USA
 
Join Date: Aug 2007
Posts: 174
Send a message via AIM to cwisbg
variables, buttons and gui question

i put together a little example of a problem im running into while learning gui stuff. My problem was passing the newly created x variable into a second function.

My first attempt at defining the x as global outside function failed and the x was not seen. but when creating it inside the function it worked. Just wondering if im doing something wrong with this?

Is there a different way to pass my variables that are called though button?


Code:
from pymel.core import * #global x def hi(): global x x = "Hello" print x return x def hiThere(*args): print x , "is", len(x), "long" window = window(t = "Test return") layout = frameLayout() #hiBut = button(l = "Hello", command = Callback(x = hi)) hiBut = button(l = "Hello", command = Callback(hi)) returnBut = button(l = "return",command = Callback(hiThere, x)) showWindow( window )
__________________
www.cwisbg.com

Last edited by cwisbg : 09-30-2012 at 02:32 AM.
 
Old 10-01-2012, 02:43 PM   #2
haggi
Expert
3d freelancer
 
Join Date: Apr 2004
Posts: 952
Quote:
Originally Posted by cwisbg
Code:
from pymel.core import * #global x def hi(): global x x = "Hello" print x return x def hiThere(*args): print x , "is", len(x), "long" window = window(t = "Test return") layout = frameLayout() #hiBut = button(l = "Hello", command = Callback(x = hi)) hiBut = button(l = "Hello", command = Callback(hi)) returnBut = button(l = "return",command = Callback(hiThere, x)) showWindow( window )


Well, if you use python, why not try it the pythonic way?

Code:
class IdButtonWin(pm.ui.Window): _singleton = 1 def __init__(self, *args): self.title = "Test Return" self.buildUI() self.show() def sayHi(args = None): if args is None: print "Hi" else: print "Args", args def buildUI(self): with self: with pm.frameLayout(): pm.button(label='Hello', c=pm.Callback(self.sayHi)) pm.button(label='Return', c=pm.Callback(self.sayHi, someValue))
__________________
www.renderwiki.com - www.openmaya.net
 
Old 10-01-2012, 10:49 PM   #3
cwisbg
PRO
 
cwisbg's Avatar
portfolio
Brandon Gillespie
generalist
High Impact
Denver, USA
 
Join Date: Aug 2007
Posts: 174
Send a message via AIM to cwisbg
so i was messing with your code today a little and couldn't get what i wanted without errors.
Im still a little to new to be able to figure out why its not working, to me it seems like it should work, would love some pointers?


Code:
import pymel.core as pm class IdButtonWin(pm.ui.Window): _singleton = 1 # a little unclear as to what this guy is doing? is this my someValue def __init__(self, *args): self.title = "Test Return" self.buildUI() self.show() def sayHi(*args): if args is None: someValue += 1 # want to change variable with button push print "Hi" else: print "Args", someValue def buildUI(self): with self: with pm.frameLayout(): pm.button(label='Hello', c=pm.Callback(self.sayHi, someValue )) pm.button(label='Return', c=pm.Callback(self.sayHi, someValue)) someValue = 0 x = IdButtonWin() UnboundLocalError: local variable 'someValue' referenced before assignment #
__________________
www.cwisbg.com

Last edited by cwisbg : 10-01-2012 at 11:23 PM.
 
Old 10-02-2012, 09:32 PM   #4
haggi
Expert
3d freelancer
 
Join Date: Apr 2004
Posts: 952
Sorry, the _singleton is a propriatary extension to take care that a window exists only once. Just delete it.

Your problem is the "someValue" variable. You define it outside the class, what means outside the scope. This way it is unknown inside the class methods. Simply create a someValue in the init method: self.someValue = 0 and my code was crap! Sorry. This one is a bit better:

Code:
import pymel.core as pm class IdButtonWin(pm.ui.Window): def __init__(self, *args): self.title = "Test Return" self.someValue = 0 self.buildUI() self.show() def sayHi(self, args = None): if args is None: self.someValue += 1 # want to change variable with button push print "Hi" else: print "Args", self.someValue def buildUI(self): with self: with pm.frameLayout(): pm.button(label='Hello', c=pm.Callback(self.sayHi)) #no value pm.button(label='Return', c=pm.Callback(self.sayHi, 1)) x = IdButtonWin()
__________________
www.renderwiki.com - www.openmaya.net
 
Old 10-02-2012, 09:32 PM   #5
CGTalk Moderation
Lord of the posts
CGTalk Forum Leader
 
Join Date: Sep 2003
Posts: 1,066,481
Thread automatically closed

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
__________________
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
 
Thread Closed share thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
CGSociety
Society of Digital Artists
www.cgsociety.org

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump
Miscellaneous

All times are GMT. The time now is 04:37 AM.


Powered by vBulletin
Copyright ©2000 - 2016, Jelsoft Enterprises Ltd.