×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Tkinter: variable-defined object name

Tkinter: variable-defined object name

Tkinter: variable-defined object name

(OP)
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

RE: Tkinter: variable-defined object name

(OP)
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

RE: Tkinter: variable-defined object name

(OP)
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close