2017-10-17 14 views
-1

入力した内容が正しい限り、ユーザーがログインボタンを押したときに新しいフレームを読み込もうとしています。コードが正しく実行されているかどうかを確認するために、LogInCheck関数内にprintコマンドを含めました。私の唯一の問題は、フレームを変更しないことです。あなたはそれの残りの部分を見てする必要がある場合、私は私の完全なコードが含まれますPythonでtkinterを使ってログインページを作る

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

:私は私のメインクラス厥'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() 

エラーを取得し、これは、ログインページクラスです

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オーバーフローを歓迎します。あなたの状況については、[最小限の完全で検証可能な例](https://stackoverflow.com/help/mcve)を提供する必要があります。私たちが必要とするのは、フレームを前面に動かすボタンを備えた単一のウィンドウです。私たちはすべてのコードを必要としません。ちょうどその質問に関連するもの。 –

+0

表示されている問題は、LoginクラスがPhoneCompanyメソッドshow_framesを呼び出そうとしていることが原因です。 Loginクラスにはそのようなメソッドはありません。 –

+0

それで、PhoneCompanyクラスからshow_frames関数をログインクラスにコピーするのは簡単ですか?編集:私は思ったように単純ではない、それを試みた –

答えて

0

self.controller.show_frame(StartPage)である必要があります。

+0

私はこれを試して考えていないと信じられない。本当にありがとう :) –

関連する問題