2017-10-17 14 views
-1

我试图在用户按下登录按钮时加载新框架,只要输入的详细信息是正确的。我在LogInCheck函数中包含了print命令,以查看代码是否正确执行。我唯一的问题是它不会改变框架。我得到的错误'LogIn' object missing attribute 'show_frame'在python中使用tkinter制作登录页面

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() 

我知道这是相当混乱,可能有其他错误,但现在我只需要弄清楚为什么它不会在之后成功登录改变框。

+2

嗨,詹姆斯,欢迎来到堆栈溢出。对于您的情况,您应该提供[最小,完整和可验证示例](https://stackoverflow.com/help/mcve)。我们所需要的只是一个带有一个按钮的窗口,它应该将框架移动到前面。我们不需要(或不想)所有的代码。只是什么与问题有关。 –

+0

您看到的问题是由Login类试图调用PhoneCompany方法show_frames引起的。 Login类没有这样的方法。 –

+0

因此,它是否像将PhoneCompany类的show_frames函数复制到登录类一样简单?编辑:试过了,并不像我想的那么简单 –

回答

0

它需要是self.controller.show_frame(StartPage)

+0

不能相信我没有想到试试这个。非常感谢 :) –