Reputation: 129
The following is the functional issue I'm dealing with. I'm not sure how long 'frank" will be since in the real program he's being created from a line of text. What's happening is 2 things. 1) in this small demonstration, it isn't waiting for the button press and it just prints empty values for what should reflect an entry. 2) When I use this function to print to a csv, I'm getting the entry box instance as a string. I assume that fixing one will fix the other. Thanks.
from Tkinter import *
import ttk
def getThat(top,bottom):
bottom = bottom.get()
print ('%s: "%s"' % (top, bottom))
root = Tk()
root.title("This space intentionally left blank")
root.minsize(900,200)
mainframe = ttk.Frame(root)
mainframe.grid(column = 1, row = 1)
frank = ["beany", "beans", "beens", "not beets"]
x=0
for f in frank:
ttk.Label(mainframe, text=frank[x]).grid(column=x+1, row=2)
addMore = StringVar()
moreBeans = ttk.Entry(mainframe, textvariable=addMore)
moreBeans.grid(column =x+1, row =3)
ttk.Button(mainframe, text = "Do It", command = getThat(frank[x], addMore)).grid(column=1, row=5, pady=5)
x+=1
root.mainloop()
Upvotes: 0
Views: 221
Reputation: 129
In case you were wondering what the fix was, I'll post it here
from Tkinter import *
import ttk
import nltk
import csv
frank = ["beany", "beans", "beens", "not beets"]
letters = ["a","s","d","f"]
newInputs=[]
allTheBeans=[]
def getThat(newInputs):
for inputs in newInputs:
allTheBeans.append((inputs[0], inputs[1].get()))
print allTheBeans
def makeBoxes (root, frank):
for x in range(0, len(frank)):
Label(mainframe, text=frank[x]).grid(column=x+1, row=2)
moreBeans = ttk.Entry(mainframe)
moreBeans.grid(column =x+1, row =3)
moreBeans.insert(0,letters[x])
newInputs.append((frank[x],moreBeans))
return newInputs
def clearItOut(clearList):
del clearList [:]
root = Tk()
root.title("This space intentionally left blank")
root.minsize(900,200)
mainframe = ttk.Frame(root)
mainframe.grid(column = 1, row = 1)
madeBoxes=makeBoxes(mainframe, frank)
ttk.Button(mainframe, text = "Do It", command = lambda *a: getThat(newInputs)).grid(column=1, row=5)
ttk.Button(mainframe, text = "Undo It", command = lambda *a: clearItOut(allTheBeans)).grid(column=1, row=6)
root.mainloop()
I added some other stuff to make sure I was understanding those principals as well.
Upvotes: 0
Reputation: 113940
I think
ttk.Button(mainframe, text = "Do It", command = lambda *a: getThat(frank[x], addMore)).grid(column=1, row=5, pady=5)
should fix it
the problem is its being called on creation ... by putting it in a lambda it wont ca;ll until the click
alternatively and probably better form would be to create a function
def OnClick(*args):
return getThat(frank[x], addMore)
ttk.Button(mainframe, text = "Do It", command =OnClick).grid(column=1, row=5, pady=5)
since you are in a loop you might need to do something like
def OnMyClick(x,addMore,*args):
return getThat(frank[x], addMore)
import functools
for x,f in enumerate(frank):
...
my_listener = functools.partial(OnMyClick,x,addMore)
ttk.Button(mainframe, text = "Do It", command = my_listener)
...
Upvotes: 1