Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tkinter: variable-defined object name

Status
Not open for further replies.

arcsecond

Programmer
Jan 20, 2006
5
0
0
US
Hello,

I want to create a loop that creates Buttons and Entries. I figured I'd loop through giving them iterative names; button1 entry1Min entry1Max button2 entry2Min entry2Max and so forth.

I've got it making the widgets:
Code:
for i in range(1, int(numOfScenes) + 1):
			sceneName = ("Sc_" + str(i).zfill(padding))
			self.jpSSR_CreateScene(sceneName, i)


def jpSSR_CreateScene(self, sceneName, number):
		sceneNameButton = (sceneName + "Button")
		
		self.sceneNameButton = Button(self.buttonFrame, text=sceneName, command=self.collectData(sceneName))
		self.sceneNameButton.grid(row=(number + 2), column=0, sticky=E+W)

But I think this is wrong. It doesn't seem to be recognizing that it's supposed to use the variables. Instead of me getting a button called (sceneName + "Button") I think I get a button called sceneNameButton, the variable's name.

So how does one make a loop that returns unique object names? I've tried variable substitution: self.%s=Button() % sceneName. But that just gives me a syntax error.

I hope this makes sense. Any help would be appreciated. Thanks in advance.

-James
 
Well, turns out it looks like I can't do what I wanted to do. So I cheated and found a way around it. In case anyone else runs up against this, here's how I did it.

Code:
self.madeButtons = []
for i in range(1, int(numOfScenes) + 1):
	sceneName = ("sc_" + str(i).zfill(padding))
	self.madeButtons.append(self.jpSSR_CreateScene(sceneName, i))

def jpSSR_CreateScene(self, sceneName, number):
	self.sceneNameButton = Button(self.buttonFrame, name=sceneName, text=sceneName)
	#*****MAGIC*****
	#replaces "command=" which seems to execute immediately upon creation of button
	event_handler = event_lambda( self.collectData, number ) 
	self.sceneNameButton.bind("<ButtonRelease-1>", event_handler )
	#*****END MAGIC*****
	widgetList = [self.sceneNameButton]
	return widgetList

I then can iterate through the self.madeButtons array (which can be multi-dimensional) in order to access the objects I've made. Woo Hoo!

-James
 
I suppose I could have just made a new widget class that created my Button and my Entries (call it buttonMinMax class) and looped through creating instances of this new class and given it methods to get the values from the min and max Enties. Then iterated through a single-dimensional array of my buttonMinMax objects.

But I'm new to this whole wacky Object-Oriented thingy. Maybe I'll try that next.

-James
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top