-1

I created a new function which calls both functions. I'm trying to make a log in system which checks for the username and password first. If the username and password are correct I want it to switch to the next page, which is 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)

When I run the program this is the error I get.

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
4

1 回答 1

1

因为 callbooth 是类中的一个方法,所以你不能在不指定 self 的情况下调用它。

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

于 2016-10-21T09:30:33.140 回答