Tkinter多GUI

时间:2018-12-15 19:31:45

标签: python python-3.x tkinter

嘿,在将两个Gui连接在一起时遇到问题,因为当我按下按钮时,我希望tkinter从另一个文件中打开另一个GUI。

文件一包含前端仪表板,当您单击教师而不是单击登录时,它给我一个错误提示。

Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__ return self.func(*args) TypeError: __init__() missing 1 required positional argument: 'master'

这是主页源代码:

from tkinter import *
from login import *
import tkinter as tk
from tkinter import ttk    

class Frontend(tk.Tk):
      def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.title('Portal') # set the title of the main window
        self.geometry('300x300') # set size of the main window to 300x300 pixels

        # this container contains all the pages
        container = tk.Frame(self)
        container.pack(side='top', fill='both', expand=True)
        container.grid_rowconfigure(0, weight=1)   # make the cell in grid cover the entire window
        container.grid_columnconfigure(0,weight=1) # make the cell in grid cover the entire window
        self.frames = {} # these are pages we want to navigate to

        for F in (Dashboard, Teacher, Student): # for each page
             frame = F(container, self) # create the page
             self.frames[F] = frame  # store into frames
             frame.grid(row=0, column=0, sticky='nsew') # grid it to container    
             self.show_frame(Dashboard) # let the first page is Dashboard

     def show_frame(self, name):
         frame = self.frames[name]
         frame.tkraise()

class Dashboard(tk.Frame):

     def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        label = ttk.Label(self, text="Main Screen", font=LARGE_FONT)
        label.pack(pady=10,padx=10)
        button = ttk.Button(self, text="Teacher",
                            command=lambda: controller.show_frame(Teacher))
        button.pack()

        button1 = ttk.Button(self, text="Student",
                            command=lambda: controller.show_frame(Student))
        button1.pack()


class Teacher(tk.Frame):

     def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = ttk.Label(self, text="Teacher Login", font=LARGE_FONT)
        label.pack(pady=10,padx=10)

        #logo = ImageTk.PhotoImage(Image.open('logo.png'))

        button = ttk.Button(self, text="Login", command=LoginFrame)
        button.pack()


        button1 = ttk.Button(self, text="Home",
        command=lambda: controller.show_frame(Dashboard))
        button1.pack()


class Student(tk.Frame):

     def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = ttk.Label(self, text="Student Login", font=LARGE_FONT)
        label.pack(pady=10,padx=10)

        button = ttk.Button(self, text="Home",
                            command=lambda: controller.show_frame(Dashboard))
        button.pack()

def main():
   root = Frontend()
   root.mainloop()

if __name__=='__main__':
    main()

登录源代码:

from tkinter import *
import tkinter.messagebox as tm


class LoginFrame(Frame):
    def __init__(self, master):
        super().__init__(master)

        self.label_username = Label(self, text="Username")
        self.label_password = Label(self, text="Password")

        self.entry_username = Entry(self)
        self.entry_password = Entry(self, show="*")

        self.label_username.grid(row=0, sticky=E)
        self.label_password.grid(row=1, sticky=E)
        self.entry_username.grid(row=0, column=1)
        self.entry_password.grid(row=1, column=1)

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

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

        self.pack()

    def _login_btn_clicked(self):
        # print("Clicked")
        username = self.entry_username.get()
        password = self.entry_password.get()

        # print(username, password)

        if username == "admin" and password == "admin123":
            tm.showinfo("Login info", "Welcome Admin")
        else:
            tm.showerror("Login error", "Incorrect username")

def main():
    root = Tk()
    page = LoginFrame(root)
    root.mainloop()

if __name__=='__main__':
    main()

谢谢

0 个答案:

没有答案