2017-02-16 73 views
0

我正在尝试使用多个框架的即时通讯工具,并从我一直关注的教程中引入了一些代码。我的输入“e”应该是将用户输入保存在文本文件中,然后将其显示在文本框中,但是当我调用e.get()时,出现以下错误。它将e视为属性而不是对象(我认为),我不知道为什么。Tkinter:无法调用对象,因为它缺乏属性

Exception in Tkinter callback 
Traceback (most recent call last): 
    File "C:\Users\Douglas Rouse\AppData\Local\Programs\Python\Python35-32\lib\idlelib\run.py", line 119, in main 
    seq, request = rpc.request_queue.get(block=True, timeout=0.05) 
    File "C:\Users\Douglas Rouse\AppData\Local\Programs\Python\Python35-32\lib\queue.py", line 172, in get 
    raise Empty 
queue.Empty 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "C:\Users\Douglas Rouse\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 1550, in __call__ 
    return self.func(*args) 
    File "C:\Users\Douglas Rouse\Google Drive\Python\New structure.py", line 78, in callback 
    f.write("Douglas:"+self.e.get()+"\n") 
AttributeError: 'PageOne' object has no attribute 'e' 

我不知道为什么它认为e是一个属性或为什么不能呼叫E(入口)为对象,而不是类本身。这是我正在处理的代码。该错误在回调函数中。

import tkinter as tk 
LARGE_FONT=("Verdana", 12) 


class SeaofBTCapp(tk.Tk): 

    def __init__(self, *args, **kwargs): 
     tk.Tk.__init__(self, *args, **kwargs) 
     container = tk.Frame(self) 

     container.grid() 

     container.grid_rowconfigure(0, weight=1) 
     container.grid_columnconfigure(0, weight=1) 

     self.frames = {} 

     frame = StartPage(container, self) 
     frame_ = PageOne(container, self) 

     self.frames[StartPage] = frame 
     self.frames[PageOne] = frame_ 

     frame.grid(row=0, column=0, sticky="nsew") 
     frame_.grid(row=0, column=0, sticky="nsew") 

     self.show_frame(StartPage) 

    def show_frame(self, cont): 
     frame = self.frames[cont] 
     frame.tkraise() 

class StartPage(tk.Frame): 

    def __init__(self, parent, controller,*args): 
     tk.Frame.__init__(self, parent) 
     usrlabel = tk.Label(self, text="Input Username", font=LARGE_FONT) 
     usrlabel.grid(pady=10,padx=10) 
     usrentry = tk.Entry(self) 
     #usrentry.grid() 
     global uu 
     uu = usrentry.get() 

     #command within button cant throw args to funcs. Use lambda to throw those args to the func instead 
     button1 = tk.Button(self, text="Visit Page 1",command= 
          lambda: controller.show_frame(PageOne)) 
     button1.grid() 

class PageOne(tk.Frame): 

    def __init__(self, parent, controller): 

     tk.Frame.__init__(self, parent) 
     label = tk.Label(self, text="Page 1", font=LARGE_FONT) 
     label.grid() 

     #command within button cant throw args to funcs. Use lambda to throw those args to the func instead 
     button1 = tk.Button(self, text="Start Page",command=lambda:controller.show_frame(StartPage)) 
     button1.grid() 

     file = open("htfl.txt","r") #opens file 
     #print(file.read(1)) 
     a = file.read() 
     b = file.read() 
     print(file.read()) 

     #entry 
     e = tk.Entry(self,width= 40) 
     e.grid(row=10,column=0,sticky = "W") 
     #text 
     T = tk.Text(self, height=9, width=30) 
     T.grid(row=3,column= 0) 
     #T.insert(END,a) 
     b = tk.Button(self, text="Send", width=10, command=self.callback).grid(row=10,column=2) 

    def callback(self,*args): 
     f = open("htfl.txt","a") 
     f.write("Douglas:"+self.e.get()+"\n") 
     e.delete(0, 'end') 
     #print (e.get()) 
     #Button 



app = SeaofBTCapp() 

回答

1

e你创造PageOne__init__方法是局部变量,不能从外部引用。你想要做的就是让e成为你的对象的一个​​属性。这将导致

self.e = tk.Entry(self,width= 40) 

如果你现在要访问这个属性在你的对象,请使用self.e代替e

self.e.grid(row=10,column=0,sticky = "W") 
# -skipped- 
f.write("Douglas:"+self.e.get()+"\n") 
# -skipped- 
self.e.delete(0, 'end') 
+2

我想这个答案会更好,如果这个例子显示使用'self.e'而不是埋在下面的句子中的变化。 –

+0

@TidB我不太确定我的理解。我试着用self e.write(“Douglas:”+ self.e.get()+“\ n”)访问它。 –

+0

@douglasrouse为什么你现在正在尝试'self.e.write(...)'而不是以前'f.write(...)'? 'f'的版本很好。 – TidB