Python中的类变量检索

时间:2013-02-22 19:07:08

标签: python class textbox tkinter ttk

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

generate定义下,我可以弹出listx的名称,但我无法从{{1}的任何实例中获取本地变量entry } class。

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

2 个答案:

答案 0 :(得分:2)

两个需要更改的东西来解决您描述的问题:

  1. new_title_box.__init__()方法更改中:
    listx.append(name)
    listx.append(self.name)

  2. generate()功能中,将:folder = boxvar.entry.get()更改为folder = boxvar.get()

答案 1 :(得分:1)

您要向listx追加一个字符串,使用self.name代替本地字符串name