Question

This is a GUI I’ve been writing for a script I already have working. What I’m struggling with here is retrieving the information in the textboxes.

Under the definition generate I am able to pop a name off of listx but I am unable to grab the local variable entry from any of the instances of the new_title_box class.

from Tkinter import *
import ttk
boxvar=""
folder=""
listx=[]
count = 1
myrow = 1

class new_title_box:
    def __init__(self,name):
        global myrow, count, listx
        self.entry = StringVar()
        self.name = name
        self.name = ttk.Entry(mainframe,width=45,textvariable=self.entry)
        self.name.grid(column=1,row=myrow+1,sticky=(N,W))
        listx.append(name)
        print(listx) ## For debugging to insure that it is working correctly, if it gives output it, this part works
        myrow = myrow + 1
        count=count+1

def make_new(*args):
    new_title_box('box'+str(count))

def generate(*args):
    global listx, boxvar
    while len(listx) > 0:
        boxvar=listx.pop(0)
        print(boxvar) ## For debugging to insure that it is working correctly, if it gives output it, this part works
        folder = boxvar.entry.get() ## Not working here
        print(folder) ## For debugging to insure that it is working correctly, if it gives output it, this part works

root = Tk()
root.title("File Maker")

mainframe = ttk.Frame(root, padding = "50 50 50 50")
mainframe.grid(column = 0,row = 0,sticky = (N, W, E, S))
mainframe.columnconfigure(0,weight=1)
mainframe.columnconfigure(0,weight=1)

add_entry = ttk.Button(mainframe,width=20, text = "add entry", command=make_new)
add_entry.grid(column=2,row=2,sticky=(N,W))
add_entry = ttk.Button(mainframe,width=20, text = "make files", command=generate)
add_entry.grid(column=2,row=3,sticky=(N,W))

root.mainloop()

Here's the traceback I'm getting:

Exception in Tkinter callback 
Traceback (most recent call last): 
  File "C:\Python33\lib\tkinter_init_.py", line 1442, in call 
    return self.func(*args) 
  File "C:\python\SampAqTkinter.py", line 28, in generate 
    folder = boxvar.entry ## Not working here 
AttributeError: 'str' object has no attribute 'entry'
Was it helpful?

Solution

There are two things that need to be changed to fix the problem you describe:

  1. In the new_title_box.__init__() method change:
      listx.append(name) to
      listx.append(self.name)

  2. In the generate() function, change:
      folder = boxvar.entry.get() to
      folder = boxvar.get().

OTHER TIPS

You are appending a string to listx, use self.name instead of the local string name

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top