2016-10-21 12 views
-1

私は両方の関数を呼び出す新しい関数を作成しました。私は最初にユーザー名とパスワードを確認するログインシステムを作ろうとしています。ユーザー名とパスワードが正しい場合は、次のページ、つまりPageOneに切り替える必要があります。同時に2つの関数を呼び出す

import tkinter as tk 
import tkinter.messagebox as tm 

LARGE_FONT= ("Verdana", 12) 


class SeaofBTCapp(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): 

      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="UserName", font=LARGE_FONT).grid(row=0, sticky="E") 

     label2 = tk.Label(self, text="Password", font=LARGE_FONT).grid(row=1, sticky="E") 

     entry = tk.Entry(self).grid(row=0, column=1) 
     entry2 = tk.Entry(self,show="*").grid(row=1, column=1) 

     button = tk.Button(self, text="Log in", 
          command=callboth) 
     button.grid(columnspan=2) 
    def login(self, parent, controller): 


     username = entry.get() 
     password = entry1.get() 
     if username == ("A") and password == ("123"): 
      tm.showinfo("Login info","Welcome Doan") 
     else: 
      tm.showerror("Login error","Incorrect username") 

    def callboth(self, parent, controller): 
     login() 
     controller.show_frame(PageTwo) 

私がプログラムを実行すると、これは私が得るエラーです。

Traceback (most recent call last): 
    File "H:/A2 Computing/ddd2.py", line 162, in <module> 
app = SeaofBTCapp() 
    File "H:/A2 Computing/ddd2.py", line 24, in __init__ 
frame = F(container, self) 
    File "H:/A2 Computing/ddd2.py", line 50, in __init__ 
command=callboth) 
NameError: global name 'callboth' is not defined 
+2

'コマンド= callboth'にエラーマークされた行 - >'コマンド= self.callboth' – falsetru

+0

コールバックはパラメータを持っています。しかし、彼らは議論で呼び出されることはありません。 'parent'、' controller'を属性として '__init__'に保存し、その属性をコールバックで使う必要があるかもしれません。 – falsetru

答えて

1

callboothはクラス内のメソッドなので、selfを指定しないと呼び出すことはできません。

変更command=self.callboth

関連する問題