pymel buttons and gui question

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

THREAD CLOSED
 
Thread Tools Search this Thread Display Modes
Old 09 September 2012   #1
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?



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 September 2012 at 02:32 AM.
 
Old 10 October 2012   #2
Originally Posted by cwisbg:

 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?


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 October 2012   #3
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?



   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 October 2012 at 11:23 PM.
 
Old 10 October 2012   #4
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:


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 October 2012   #5
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



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 06:06 AM.


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