在python中使用tkinter创建登录页面

时间:2017-10-17 13:54:04

标签: python python-3.x tkinter

只要输入的详细信息正确,我就会在用户按下登录按钮时尝试加载新框架。我在'LogIn' object missing attribute 'show_frame'函数中包含from tkinter import * from tkinter import ttk import tkinter as tk LARGE_FONT= ("Arial", 16) SMALL_FONT= ("Arial", 12) current_tariff = None def tariff_A(): global current_tariff current_tariff= "A" def tariff_B(): global current_tariff current_tariff= "B" def tariff_C(): global current_tariff current_tariff= "C" class PhoneCompany(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, PageFour, PageFive, LogIn): frame = F(container, self) self.frames[F] = frame frame.grid(row=0, column=0, sticky="nsew") self.show_frame(LogIn) def show_frame(self, cont): frame = self.frames[cont] frame.tkraise() 命令,以查看代码是否正确执行,它是什么。我唯一的问题是它不会改变框架。我收到错误 class LogIn(tk.Frame): def LogInCheck(self): global actEntry global pinEntry act = "james" pin = "Python123" actNum = actEntry.get() pinNum = pinEntry.get() print("FUNCTION RUN") if actNum == act and pinNum == pin: print("CORRECT") self.show_frame(StartPage) elif actNum != act or pinNum != pin: print("INCORRECT") self.show_frame(LogIn) def __init__(self, parent, controller): global actEntry global pinEntry self.controller = controller tk.Frame.__init__(self, parent) logLabel = ttk.Label(self, text = "Login With Your Username and Password", font = LARGE_FONT) logLabel.pack(side = TOP, anchor = N, expand = YES) actLabel = Label(self, text = 'Username:') actEntry = Entry(self) pinLabel = Label(self, text = 'Password: ') pinEntry = Entry(self, show ="*") actLabel.pack(pady =10, padx = 10, side = TOP, anchor = N) pinLabel.pack(pady =5, padx = 10, side = TOP, anchor = S) actEntry.pack(pady =10, padx = 10, side = TOP, anchor = N) pinEntry.pack(pady =5, padx = 10, side = TOP, anchor = S) logInButton = tk.Button(self, text = "Login", command = self.LogInCheck) logInButton.pack(side = TOP, anchor = S) quitButton = tk.Button(self, text = "Quit", command = quit) quitButton.pack(side = BOTTOM, anchor = S) if __name__ == "__main__": app = PhoneCompany() app.mainloop()

from tkinter import *
from tkinter import ttk

import tkinter as tk

LARGE_FONT= ("Arial", 16)
SMALL_FONT= ("Arial", 12)
current_tariff = None


def tariff_A():
    global current_tariff
    current_tariff= "A"

def tariff_B():
    global current_tariff
    current_tariff= "B"

def tariff_C():
    global current_tariff
    current_tariff= "C"


class PhoneCompany(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, PageFour, PageFive, LogIn):

            frame = F(container, self)

            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(LogIn)

    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="MENU", font=LARGE_FONT)
        label.pack(pady=10,padx=10)

        button = tk.Button(self, text="View Account Balance",
                            command=lambda: controller.show_frame(PageOne))
        button.pack()

        button2 = tk.Button(self, text="Display Current Tariff",
                            command=lambda: controller.show_frame(PageTwo))
        button2.pack()

        button3 = tk.Button(self, text="View List of Rates",
                           command=lambda: controller.show_frame(PageThree))
        button3.pack()

        button4 = tk.Button(self, text="View Latest Bill",
                            command=lambda: controller.show_frame(PageFour))
        button4.pack()


class PageOne(tk.Frame):

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

        sublabel = tk.Label(self, text="Your current account balance is £230", font=SMALL_FONT)
        sublabel.pack(pady=10,padx=10)

        button1 = tk.Button(self, text="Back to Menu",
                           command=lambda: controller.show_frame(StartPage))
        button1.pack()

class PageTwo(tk.Frame):

    def __init__(self, parent, controller):
        global current_tariff
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Current Tariff", font=LARGE_FONT)
        label.pack(pady=10,padx=10)

        sublabel = tk.Label(self, text="Your current tariff is "+str(current_tariff), font=SMALL_FONT)
        sublabel.pack(pady=10,padx=10)

        button1 = tk.Button(self, text="Change Tariff",
                            command=lambda: controller.show_frame(PageFive))
        button1.pack()

        button2 = tk.Button(self, text="Back to Home",
                           command=lambda: controller.show_frame(StartPage))
        button2.pack()

class PageThree(tk.Frame):

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

        sublabel = tk.Label(self, text="Peak Rates: A: £0.30 | B: £0.10 | C: £0.90", anchor="w", font=SMALL_FONT)
        sublabel.pack(pady=10,padx=10)

        sublabel2 = tk.Label(self, text="Off Peak: A: £0.05 | B: £0.02 | C: -", anchor="w", font=SMALL_FONT)
        sublabel2.pack(pady=10,padx=10)

        sublabel3 = tk.Label(self, text="Line Rental: A: £15.00 | B: £20.00 | C: £30.00", anchor="w", font=SMALL_FONT)
        sublabel3.pack(pady=10,padx=10)

        button1 = tk.Button(self, text="Back to Home",
                           command=lambda: controller.show_frame(StartPage))
        button1.pack()

class PageFour(tk.Frame):

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

        button1 = tk.Button(self, text="Back to Home",
                           command=lambda: controller.show_frame(StartPage))
        button1.pack()

class PageFive(tk.Frame):

    def __init__(self, parent, controller):
        global current_tariff
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Change Tariff", font=LARGE_FONT)
        label.pack(pady=10,padx=10)

        sublabel = tk.Label(self, text="Select new tariff\nView list of tariff rates on the main menu to see the prices.", font=SMALL_FONT)
        sublabel.pack(pady=10,padx=10)

        button1 = tk.Button(self, text="A",
                            command=lambda:[controller.show_frame(StartPage),tariff_A])
        button1.pack()

        button2 = tk.Button(self, text="B",
                            command=lambda:[controller.show_frame(StartPage),tariff_B])
        button2.pack()

        button3 = tk.Button(self, text="C",
                            command=lambda:[controller.show_frame(StartPage),tariff_C])
        button3.pack()

        button4 = tk.Button(self, text="Back to Home",
                            command=lambda: controller.show_frame(StartPage))
        button4.pack()

class LogIn(tk.Frame):

    def LogInCheck(self):
        global actEntry
        global pinEntry

        act = "james"
        pin = "Python123"

        actNum = actEntry.get()
        pinNum = pinEntry.get()

        print("FUNCTION RUN")
        if actNum == act and pinNum == pin:
            print("CORRECT")
            self.show_frame(StartPage)
        elif actNum != act or pinNum != pin: 
            print("INCORRECT")
            self.show_frame(LogIn)

    def __init__(self, parent, controller):

        global actEntry
        global pinEntry
        self.controller = controller

        tk.Frame.__init__(self, parent)

        logLabel = ttk.Label(self, text = "Login With Your Username and Password", font = LARGE_FONT)
        logLabel.pack(side = TOP, anchor = N, expand = YES)


        actLabel = Label(self, text = 'Username:')
        actEntry = Entry(self)

        pinLabel = Label(self, text = 'Password: ')
        pinEntry = Entry(self, show ="*")

        actLabel.pack(pady =10, padx = 10, side = TOP, anchor = N)
        pinLabel.pack(pady =5, padx = 10, side = TOP, anchor  = S)

        actEntry.pack(pady =10, padx = 10, side = TOP, anchor = N)
        pinEntry.pack(pady =5, padx = 10, side = TOP, anchor  = S)

        #  runs the 'LoginCheck' function

        logInButton = tk.Button(self, text = "Login",
                                 command = self.LogInCheck)
        logInButton.pack(side = TOP, anchor = S)

        quitButton = tk.Button(self, text = "Quit", command = quit)
        quitButton.pack(side = BOTTOM, anchor = S)


if __name__ == "__main__":
    app = PhoneCompany()
    app.mainloop()

这是我的主要课程,这是登录页面类:

Report

如果您需要查看其余部分,我将包含我的完整代码:

public function reportedItem()
{
    return $this->belongsTo('App\Item', 'item_id', 'id');
}

我知道它非常混乱并且可能存在其他错误,但是现在我只需要弄清楚为什么它在成功登录后不会改变框架。

1 个答案:

答案 0 :(得分:0)

需要self.controller.show_frame(StartPage)