2017-09-16 4 views
0

ログインページを持つTkinter Python progarmを作っています。ログインすると、ユーザーが対話するためのメニューページが表示されます。Tkinterログインとメニュープログラム

次のようにクイズクラス、スタートページクラス、Menuクラス、難易度クラス、およびログイン・ページが含まれて私のコードは次のとおりです。

import tkinter as tk 
from tkinter import * 
import tkinter.messagebox as tm 
class Quiz (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, Menu, Difficulty): 
      frame = F(container, self) 
      self.frames[F] = frame 
      frame.grid (row = 0, column = 0 , sticky = "nsew") 
     self.show_frame(StartPage) 
    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 = "Login") 
     label.pack(pady = 10 , padx = 10) 
     Username = tk.Entry(self) 
     Username.pack() 
     Password = tk.Entry (self, show = "*") 
     Password.pack() 
     button1 = tk.Button(self, text = "Login", command = lambda: Login(Username,Password,parent,controller,self)) 
     button1.pack() 

class Menu(tk.Frame): 
    def __init__ (self, parent, controller) : 
     tk.Frame.__init__(self, parent) 
     label = tk.Label(self, text = "Menu") 
     button1 = tk.Button(self, text = "Histoy", command = lambda: controller.show_frame(Difficulty)) 
     button1.pack() 
     button2 = tk.Button(self, text = "Maths", command = lambda: controller.show_frame(Difficulty)) 
     button2.pack() 
     button3 = tk.Button(self, text = "Music", 
         command = lambda: controller.show_frame(Difficulty)) 
     button3.pack() 

class Difficulty(tk.Frame): 
    def __init__ (self, parent, controller): 
     tk.Frame.__init__(self,parent) 
     label = tk.Label(self, text = "Difficulty") 
     Easy = tk.Button(self, text = "Easy", command = lambda: controller.show_frame(Difficulty)) 
     Easy.pack() 
     Medium = tk.Button(self, text = "Medium",command = lambda: controller.show_frame(Difficulty)) 
     Medium.pack() 
     Hard = tk.Button(self, text = "Hard", command = lambda: controller.show_frame(Difficulty)) 
     Hard.pack() 
     backtomenu = tk.Button(self, text = "Back to Menu", command = lambda: controller.show_frame(Menu)) 
     backtomenu.pack() 

def Login(Username,Password,parent,controller,self): 
    Usernames = [] 
    count = 0 
    Username = Username.get() 
    Password = Password.get() 
    try: 
     with open ("Usernames&Passwords.txt" , "r", encoding = "UTF-8") as     file: 
      for each in file: 
       Usernames.append(each.strip("\n")) 

except IOError as error: 
    print (error) 
    if Usernames[count] == Username : 
     if Usernames[count + 1] == Password: 
      Menu(parent, controller) 
      print ("Hi") 
     else: 
      tm.showinfo("Username or Password is Incorrect") 

    else: 
      tm.showinfo("Username or Password is Incorrect") 
app = Quiz() 
app.geometry ("500x300") 
app.mainloop() 

私は、コードを実行すると、それはそこに示されたエラーメッセージはありませんが、 Menuクラスを実行しないので、次のTkinterフレームに移動してプログラムを続行しません。誰かがメインクラスが動作していない理由の正しい方向に私を指すのに役立つことができますか?前もって感謝します。

+1

に変更してください。質問の字下げを修正してください。それは私たちが続けなければならないことです。あなたのコードを適切にフォーマットする時間を取るつもりがなければ、ほとんどの人はあなたに良い答えを与える時間を取るつもりはありません。 –

答えて

-1

次のウィジェットが表示される前に、他のウィジェットをすべて破棄する必要があります。 Menu(親、コントローラ)の前にself.destroy()を挿入します。 同じことを難易度で行う必要があります。

編集:@ブライアンオークリー氏は、これは間違った解決策であると指摘しています。代わりにMenu(parent, controller)controller.show_frame(Menu)

+0

ありがとう!コード内のaboutsは、難易度セクションでもう一方のself.destory()を必要とするでしょうか? –

+0

これは正しい解決策ではありません。インスタンスは既に 'Quiz'で作成されているので、' Login'の中で 'Menu(parent、controller)'を呼び出すべきではありません。 –

関連する問題