0

これは、既に作業しているスクリプト用に作成した GUI です。ここで苦労しているのは、テキストボックス内の情報を取得することです。

定義の下ではgenerate、名前をポップすることはできますが、クラスのインスタンスからlistxローカル変数を取得することはできません。entrynew_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

あなたが説明した問題を解決するために変更する必要がある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 に答える