Tkinter:条目显示为自己的窗口

时间:2017-02-16 22:51:49

标签: python tkinter

我正在尝试在主tkinter窗口中输入一个输入字段但是如果我将e= tk.Entry(...)放入我的__init__我会收到错误。当放置在init之外时,它会创建自己的窗口。该条目工作并履行其功能,但我希望它成为主窗口的一部分。

    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'

1 个答案:

答案 0 :(得分: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()