2015-11-17 77 views
-1

我爲我的ATM模擬程序創建了一個登錄頁面框架,我無法讓我的登錄按鈕正常運行。我的登錄頁面有兩個輸入字段,帳號和密碼。如果賬戶號碼與我創建的賬戶號碼匹配,並且PIN號碼與我創建的PIN號碼匹配,我希望能夠打開一個名爲「歡迎頁面」的新窗口。如果不是,則保留在登錄頁面上。這是到目前爲止我的整個腳本,Python登錄頁面使用Tkinter

#!/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我打算用我的泡菜模塊創建帳號和引腳的字典,但現在我只想要使用這些帳戶登錄。歡迎有關如何完成此任務以及如何改進我的腳本的任何建議,謝謝!

回答

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返回,因爲它不是LogIn而是ATM的方法)。

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) 
+3

最好添加一段代碼的解釋。 – aristotll