同时调用两个函数

时间:2016-10-21 09:11:14

标签: python tkinter login-system

我创建了一个调用这两个函数的新函数。我试图建立一个登录系统,首先检查用户名和密码。如果用户名和密码正确,我希望它切换到下一页,即PageOne。

import tkinter as tk
import tkinter.messagebox as tm

LARGE_FONT= ("Verdana", 12)


class SeaofBTCapp(tk.Tk):

    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 (StartPage, PageOne, PageTwo, PageThree):

            frame = F(container, self)

            self.frames[F] = frame

            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):
        tk.Frame.__init__(self,parent)
        label = tk.Label(self, text="UserName", font=LARGE_FONT).grid(row=0, sticky="E")

        label2 = tk.Label(self, text="Password", font=LARGE_FONT).grid(row=1, sticky="E")

        entry = tk.Entry(self).grid(row=0, column=1)
        entry2 = tk.Entry(self,show="*").grid(row=1, column=1)

        button = tk.Button(self, text="Log in",
                            command=callboth)
        button.grid(columnspan=2)
    def login(self, parent, controller):


        username = entry.get()
        password = entry1.get()
        if username == ("A") and password == ("123"):
            tm.showinfo("Login info","Welcome Doan")
        else:
            tm.showerror("Login error","Incorrect username")

    def callboth(self, parent, controller):
        login()
        controller.show_frame(PageTwo)

当我运行程序时,这是我得到的错误。

Traceback (most recent call last):
  File "H:/A2 Computing/ddd2.py", line 162, in <module>
app = SeaofBTCapp()
  File "H:/A2 Computing/ddd2.py", line 24, in __init__
frame = F(container, self)
  File "H:/A2 Computing/ddd2.py", line 50, in __init__
command=callboth)
NameError: global name 'callboth' is not defined

1 个答案:

答案 0 :(得分:1)

由于callbooth是类中的一个方法,因此如果不指定self,则无法调用它。

将错误标记的行更改为command=self.callboth