Tkinter在打开新框架时遇到问题

时间:2016-01-20 18:34:11

标签: python user-interface tkinter frames

我已点击我的按钮打开ProjecMain.py点击并收到错误,但我不太了解它们以及为什么它们正在发生我将这两个页面都复制到这里。

TkintGUI.py

import tkinter as tk
import tkinter.messagebox as tm
import ProjecMain

from pip._vendor.cachecontrol import controller



class LoginFrame(tk.Frame):

    def __init__(self, *args, **kwargs):


        tk.Tk.__init__(self, *args, **kwargs)

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (LoginScreen,ProjecMain):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")
            self.show_frame(LoginScreen)

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()

class LoginScreen(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        self.label_1 = tk.Label(self, text="Username")
        self.label_2 = tk.Label(self, text="Password")

        self.entry_1 = tk.Entry(self)
        self.entry_2 = tk.Entry(self, show="*")


        self.label_1.grid(row=0)
        self.entry_1.grid(row=0, column=1)
        self.label_2.grid(row=1)
        self.entry_2.grid(row=1, column=1)

        self.checkbox = tk.Checkbutton(self, text="Keep me logged in")
        self.checkbox.grid(columnspan=2)

        self.logbtn = tk.Button(self, text="Login", command=self.login_btn)
        self.logbtn.grid(columnspan=2)

        self.pack()


def login_btn(self):
        #print("On click action/event")
        Username = self.entry_1.get()
        Password = self.entry_2.get()

        if Username == "Lewis" and Password == "starwars10":
            lambda:controller.show_frame(ProjecMain)
        else:
            tm.showerror("Login error", "Incorrect Login details")


app = LoginFrame()
app.mainloop()

ProjecMain.py

import os
import tkinter as tk
import tkinter.messagebox as tm

import TkintGUI



class ProjecMain(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.Random_label = tk.Label(self, text="Random Label")



app = ProjecMain()
app.mainloop()

这些是我收到的错误

    Traceback (most recent call last):
  File "C:\Users\Lewis Collins\Python Project\GUI\TkintGUI.py", line 6, in <module>
    from GUI import ProjecMain
  File "C:\Users\Lewis Collins\Python Project\GUI\ProjecMain.py", line 12, in <module>
    app = ProjecMain()
TypeError: __init__() missing 2 required positional arguments: 'parent' and 'controller'

使用退出代码1完成处理

1 个答案:

答案 0 :(得分:1)

错误告诉你到底出了什么问题:

  

TypeError: init ()缺少2个必需的位置参数:'parent'和'controller'

这意味着ProjectMain需要2个位置参数,一个名为parent,一个名为controller。您正在ProjectMain.py底部的这行代码中创建没有这些参数的类:

app = ProjectMain()

如果您打算在TkintGUI.py中使用ProjectMain,则必须删除该行,并在ProjectMain.py中调用mainloop()。您的主应用程序负责创建此类的实例。