我正在尝试制作一个具有多个帧的即时消息,并且已经合并了我一直关注的教程中的一些代码。我的条目" e"应该将用户输入保存在文本文件中并将其显示在文本框中但是当我调用e.get()时出现以下错误。它将e视为属性而不是对象(我认为)并且我不知道为什么。
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()
答案 0 :(得分:1)
您在e
的{{1}}方法中创建的PageOne
是一个局部变量,无法从外部引用。您要做的是使__init__
成为对象的属性。这将导致
e
如果您现在想要在对象中访问此属性,请使用self.e = tk.Entry(self,width= 40)
代替self.e
。
e