2017-02-16 24 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

相關問題