Python登录页面使用Tkinter

时间:2015-11-17 17:38:49

标签: python user-interface python-3.x login tkinter

我为自己的ATM模拟程序创建了一个登录页框,我无法让我的登录按钮正常运行。我的登录页面有两个输入字段,帐号和PIN号。如果帐号与我创建的帐号匹配,并且PIN号与我创建的PIN码匹配,我希望能够打开我创建的名为“欢迎页面”的新框架窗口。如果不是,它将保留在登录页面上。到目前为止,这是我的整个脚本,

#!/usr/bin/python


from tkinter import *
from tkinter import ttk

import tkinter as tk


Large_Font = ("Verdana", 18)
Small_Font = ("Verdana", 12)

act = '1234567'
pin = '1234'


class ATM(tk.Tk):
    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)
        tk.Tk.wm_title(self, "ATM Simulator")

        #tk.Tk.iconbitmap(self, default = "atm.ico")

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

        self.frames = {}

        for i in (LogIn, WelcomePage, Checking, Savings, Transfer):

            frame = i(container, self)
            self.frames[i] = frame 
            frame.grid(row= 100, column = 100, sticky= "nsew")

        self.show_frame(LogIn)


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

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

        global actEntry
        global pinEntry

        tk.Frame.__init__(self, parent)

        logLabel = ttk.Label(self, text = "Login With Your Account Number and Pin", font = Large_Font)
        logLabel.pack(side = TOP, anchor = N, expand = YES)


        actLabel = Label(self, text = 'Account Number:')
        pinLabel = Label(self, text = 'PIN Number: ')

        actEntry = Entry(self)
        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 = ttk.Button(self, text = "Enter",
                                 command = self.LogInCheck)
        logInButton.pack(side = TOP, anchor = S)

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

    def LogInCheck(self):
        actNum = actEntry.get()
        pinNum = pinEntry.get()

        if actNum == act and pinNum == pin:
            return 
            self.show_frame(WelcomePage)
        else: 
            return
            self.show_frame(LogIn)


class WelcomePage(tk.Frame):

    #Welcome Page Window

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = ttk.Label(self, text = "Welcome to the ATM Simulator", font = Large_Font)
        label.pack(pady=100, padx=100)

        checkButton = ttk.Button(self, text = "Checking Account", 
                             command = lambda: controller.show_frame(Checking))
        checkButton.pack()

        saveButton = ttk.Button(self, text = "Savings Account", 
                            command = lambda: controller.show_frame(Savings))
        saveButton.pack()

        transButton = ttk.Button(self, text = "Transfer Funds", 
                            command = lambda: controller.show_frame(Transfer))
        transButton.pack()

        quitButton = ttk.Button(self, text = "End Transaction", command = self.client_exit)
        quitButton.pack()

    def client_exit(self):
        exit()

class Checking(tk.Frame):

    #Checking Account Window

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text = "Checking Account", font = Large_Font)
        label.pack(pady=100, padx=100)

        homeButton = ttk.Button(self, text = "Back to Home Page", 
                             command = lambda: controller.show_frame(WelcomePage))   
        homeButton.pack()
        quitButton = ttk.Button(self, text = "End Transaction", command = quit)
        quitButton.pack()


class Savings(tk.Frame):

    #Savings Account Window

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = ttk.Label(self, text = "Savings Account", font = Large_Font)
        label.pack(pady=100, padx=100)

        homeButton = ttk.Button(self, text = "Back to Home Page", 
                             command = lambda: controller.show_frame(WelcomePage))   
        homeButton.pack()
        quitButton = ttk.Button(self, text = "End Transaction", command = quit)
        quitButton.pack()


class Transfer(tk.Frame):

    #Transfer Funds Window

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = ttk.Label(self, text = "Transfer Funds", font = Large_Font)
        label.pack(pady=100, padx=100)

        homeButton = ttk.Button(self, text = "Back to Home Page", 
                             command = lambda: controller.show_frame(WelcomePage))   
        homeButton.pack()
        quitButton = ttk.Button(self, text = "End Transaction", command = quit)
        quitButton.pack()

atm = ATM()
atm.mainloop()    

默认帐号1234567和我创建的默认密码是1234.我计划使用pickle模块创建帐号和密码字典但是现在我只想用这些帐户登录。我欢迎任何有关如何完成此任务以及如何改进我的脚本的建议,谢谢!

2 个答案:

答案 0 :(得分:4)

很可能会出现更多问题,但请看一下:

    if actNum == act and pinNum == pin:
        return 
        self.show_frame(WelcomePage)
    else: 
        return
        self.show_frame(LogIn)

在两个(if / else)中,你在做实际工作之前打电话给return! self.show_frame()在任何情况下都不会被调用(并且它也不会被称为wo return,因为它不是LogIn的方法,而是ATM)。

答案 1 :(得分:0)

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

    def LogInCheck(self):
        ...
        if actNum == act and pinNum == pin:
            return self.controller.show_frame(WelcomePage)
        else:
            return self.controller.show_frame(LogIn)