2017-04-25 9 views
0

現在、tkinterでフレームを使用してログインページを作成しようとしていて、 'Frame'オブジェクトに 'entry_U'という属性がありません。私はいくつかの同様の質問を見てきましたが、答えのどれも私のコードに適用できるように思わない:私は、一般的なプログラミングに非常に新しいですTkinter 'Frame'オブジェクトに 'entry_U'という属性がありません

import tkinter as tk 
from tkinter import ttk 

root = tk.Tk() 
def Page1(): 
    f1.pack() 
    f2.pack_forget() 
    f3.pack_forget() 

def Page2(): 
    f1.pack_forget() 
    f2.pack() 
    f3.pack_forget() 

def Page3(): 
    f1.pack_forget() 
    f2.pack_forget() 
    f3.pack() 

root.title('Frames') 

f1 = tk.Frame(root) 
label_1 = tk.Label(f1, text='Login') 
label_1.pack() 

label_U = tk.Label(f1, text="Username") 
label_U.pack() 
entry_U = tk.Entry(f1) 
entry_U.pack() 

label_P = tk.Label(f1, text="Password") 
label_P.pack() 
entry_P = tk.Entry(f1, show="*") 
entry_P.pack() 

checkbox = tk.Checkbutton(f1, text="Keep me logged in") 
checkbox.pack() 
#error here 
username = f1.entry_U.get() 
password = f1.entry_P.get() 

if username == "user1" and password == "password": 
    userid == True 
else: 
    userid == False 

if userid == True: 
    logbtn = tk.Button(f1, text="Login", command = Page2) 
    logbtn.pack() 
else:     
    tk.messagebox.showerror("Login error", "Incorrect username or password") 


but_quit = tk.Button(f1, text='Quit', command = quit) 
but_quit.pack() 

f2 = tk.Frame(root) 
label_2 = tk.Label(f2, text='Page2') 
label_2.pack() 
but_3 = tk.Button(f2, text='Go to Page 3', command = Page3) 
but_3.pack() 
but_quit = tk.Button(f2, text='Quit', command = quit) 
but_quit.pack() 

f3 = tk.Frame(root) 
label_3 = tk.Label(f3, text='Page3') 
label_3.pack() 
but_1 = tk.Button(f3, text='Go to Page 1', command = Page1) 
but_1.pack() 
but_quit = tk.Button(f3, text='Quit', command = quit) 
but_quit.pack() 

f1.pack() 
root.mainloop() 

と私がやろうとしていることはすべてのヘルプは可能ですが、場合見当がつかない感謝されます。ありがとう。

答えて

1

entry_Uがマスターがf1(フレーム)のウィジェットであっても、 'entry_U'という名前は単に 'f1'という名前とは特に関係のないグローバル名です。 'f1。'を削除します。接頭辞。

1

単にentry_Uがマスターウィジェットとしてf1を使用しているため、そのウィジェットの属性にはなりません。

username = entry_U.get() 

また、ログインコードをPage2関数に移動する必要があります。

関連する問題