2017-02-16 64 views
-2

我想在我的主tkinter窗口中获得输入字段,但是如果我将e= tk.Entry(...)放入我的__init__中,则会出现错误。当放置在init之外时,它会创建自己的窗口。入口工作并履行其功能,但我希望它成为主窗口的一部分。Tkinter:条目显示为自己的窗口

class ChatApp(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): 
     e = tk.Entry(self,tk.Frame,width= 40) 
     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 

     self.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): 
     global uu 
     f = open("htfl.txt","a") 
     f.write("Douglas:"+self.e.get()+"\n") 
     self.e.delete(0, 'end') 

我试图把e = tk.Entry(self,width= 40)__init__并得到了以下错误

Traceback (most recent call last): 
    File "C:\Users\Douglas Rouse\Google Drive\Python\New structure.py", line 94, in <module> 
    app = ChatApp() 
    File "C:\Users\Douglas Rouse\Google Drive\Python\New structure.py", line 19, in __init__ 
    frame_ = PageOne(container, self) 
    File "C:\Users\Douglas Rouse\Google Drive\Python\New structure.py", line 52, in __init__ 
    e = tk.Entry(self,width= 40) 
    File "C:\Users\Douglas Rouse\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 2516, in __init__ 
    Widget.__init__(self, master, 'entry', cnf, kw) 
    File "C:\Users\Douglas Rouse\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 2132, in __init__ 
    BaseWidget._setup(self, master, cnf) 
    File "C:\Users\Douglas Rouse\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 2110, in _setup 
    self.tk = master.tk 
AttributeError: 'PageOne' object has no attribute 'tk' 
+0

你试过把它在'__init__'并指定根窗口,即'self.e = tk.Entry(个体经营,宽度= 40)'?我的意思是,你用'label'做了。 – TigerhawkT3

+0

@ TigerhawkT3是的,然后我也得到一个错误。 –

+2

请不要编辑你的问题,并彻底改变你问的问题。它使我的答案毫无用处。此外,请阅读并遵循这里的建议:[如何创建一个最小,完整和可验证的示例](http://stackoverflow.com/help/mcve) –

回答

0

我打电话初始化框架本身之前的条目。所有我需要做的就是:

def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     self.e = tk.Entry(self,width= 40) 
     label = tk.Label(self, text="Page 1", font=LARGE_FONT) 
     label.grid()