Tkinter

J

Jay

I wold like to be able to generate buttons from a list in a file.
How would I go about making each button have a different command,
Lets say make the button print its label instead of print "."

The problem I have is I don't know what the list is going to be until
it is opened or how big so I carnet make a def for each.

And I don't know how to do it with a loop

Thanks

------Code-------

from Tkinter import *

class App:
def __init__(self, root):

self.DisplayAreaListsFrame = Frame(root)
self.DisplayAreaListsFrame.pack()

Lists = ["A1","B2","C3","D4"]

for i in Lists:
self.DisplayAreaListButton = Button(
self.DisplayAreaListsFrame,
text=i,
command=self.ListButtonCommand)
self.DisplayAreaListButton.pack(side=LEFT,padx=10)

def ListButtonCommand(self):
print "."

root = Tk()
app = App(root)
root.mainloop()
 
F

Fredrik Lundh

Jay said:
I wold like to be able to generate buttons from a list in a file.
How would I go about making each button have a different command,
Lets say make the button print its label instead of print "."

The problem I have is I don't know what the list is going to be until
it is opened or how big so I carnet make a def for each.

def is an executable statement, and creates a new function object
everytime it's executed. if you want a new function for each pass
through a loop, just put the def inside the loop:
from Tkinter import *

class App:
def __init__(self, root):

self.DisplayAreaListsFrame = Frame(root)
self.DisplayAreaListsFrame.pack()

Lists = ["A1","B2","C3","D4"]

for i in Lists:

def callback(text=i):
print text
self.DisplayAreaListButton = Button(
self.DisplayAreaListsFrame,
text=i,
command=callback)
self.DisplayAreaListButton.pack(side=LEFT,padx=10)

root = Tk()
app = App(root)
root.mainloop()

the only think that's a little bit tricky is scoping: you can access all
variables from the __init__ method's scope from inside the callbacks,
but the callbacks will be called after the loop is finished, so the "i"
variable will be set to the *last* list item for all callbacks.

to work around this, you have to explicitly pass the *value* of "i" to
to the callback; the "text=i" part in the above example uses Python's
default argument handling to do exactly that.

</F>
 
J

Jay

I still dont quite get it, is their sum kind of way you can [def Newdef
+ i()] for example so that the def is different from the string, all
the buttons now have numbers on

If I am not mistaken Fredrik Lundh rote the tutorial Learning to
program (Python) www.freenetpages.co.uk/hp/alan.gauld/ and
www.effbot.org

If this is the same person then thank you for everything I no about
python it seams what ever I try and learn about Python it is always you
who helps or has wrote docs to help Newbie's and well everyone
really.

---code---
from Tkinter import *

class App:
def __init__(self, root):

self.DisplayAreaListsFrame = Frame(root)
self.DisplayAreaListsFrame.pack()

Lists = ["A1","B2","C3","D4"]
for i in Lists:
def i():
print i
self.DisplayAreaListButton = Button(
self.DisplayAreaListsFrame,
text=i,
command=i)
self.DisplayAreaListButton.pack(side=LEFT,padx=10)



root = Tk()
app = App(root)
root.mainloop()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top