2017-10-17 1 views
-1

입력 한 세부 정보가 올바른 경우 사용자가 로그인 버튼을 누르면 새 프레임을로드하려고합니다. 코드가 올바르게 실행되고 있는지 확인하기 위해 LogInCheck 함수 내에 print 명령을 포함 시켰습니다. 내 유일한 문제는 프레임을 변경하지 않을 것입니다. 내 메인 클래스 그게 전부 오류를 'LogIn' object missing attribute 'show_frame'파이썬에서 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

안녕하세요 James는 오버플로 스택을 환영합니다. 상황에 따라 [Minimal, Complete, Verifiable example] (https://stackoverflow.com/help/mcve)을 제공해야합니다. 우리가 필요로하는 것은 프레임을 앞쪽으로 움직여야하는 버튼이있는 하나의 창입니다. 우리는 모든 코드를 필요로하지 않습니다. 질문과 관련된 것입니다. –

+0

표시되는 문제는 Login 클래스가 PhoneCompany 메서드 show_frames를 호출하려고하기 때문에 발생합니다. Login 클래스에는 그러한 메소드가 없습니다. –

+0

그래서 PhoneCompany 클래스의 show_frames 함수를 로그인 클래스로 복사하는 것만 큼 간단합니까? 편집 : 내가 생각했던대로 간단하지 않은 그것을 시도했다. –

답변

0

self.controller.show_frame(StartPage)이어야합니다.

+0

나는 이것을 시도 생각하지 않았다 믿을 수 없어. 정말 고마워 :) –

관련 문제