0

这是我一直在为我已经工作的脚本编写的 GUI。我在这里苦苦挣扎的是检索文本框中的信息。

根据定义generate,我可以弹出一个名称,listx但我无法entry从类的任何实例中获取局部变量new_title_box

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

这是我得到的回溯:

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'
4

2 回答 2

2

要解决您描述的问题,需要更改两件事:

  1. 在方法new_title_box.__init__()改变:
      listx.append(name)
      listx.append(self.name)

  2. generate()函数中,将:更改
      folder = boxvar.entry.get()
      folder = boxvar.get()

于 2013-02-22T19:57:45.533 回答
1

您正在将一个字符串附加到 listx,使用self.name而不是本地字符串name

于 2013-02-22T19:18:24.070 回答