2017-01-10 47 views
-2

這是一個簡單的GUI程序,用於檢查用戶是否輸入了正確的用戶名和密碼。問題是,即使輸入正確的用戶名和密碼,即管理和祕密,它仍然輸出「無效登錄」使用Python GUI的用戶名和密碼

這是代碼:

from tkinter import * 
import tkinter.messagebox as box 

def dialog1(): 
    box.showinfo('info','Correct Login') 
def dialog2(): 
    box.showinfo('info','Invalid Login') 

window = Tk() 
window.title('Countries Generation') 

frame = Frame(window) 

Label1 = Label(window,text = 'Username:') 
Label1.pack(padx=15,pady= 5) 

entry1 = Entry(window,bd =5) 
entry1.pack(padx=15, pady=5) 

username = entry1.get() 

Label2 = Label(window,text = 'Password: ') 
Label2.pack(padx = 15,pady=6) 

entry2 = Entry(window, bd=5) 
entry2.pack(padx = 15,pady=7) 

password = entry2.get() 

if (username == 'admin' and password == 'secret'): 
    btn = Button(frame, text = 'Check Login',command = dialog1) 
else: 
    btn = Button(frame, text ='Check Login', command = dialog2) 

btn.pack(side = RIGHT , padx =5) 
frame.pack(padx=100,pady = 19) 
window.mainloop() 
+0

http://stackoverflow.com/questions/16525582/how-to-make-a-set-username-and-password-in-tkinter-python && http://stackoverflow.com/問題/ 28156719/how-can-i-integrate-tkinter-with-python-log-in-screen在這裏有一些例子可以告訴你如何去做。 – Lafexlos

+0

任何和所有事件觸發的檢查都應該通過一個函數來完成。您的if語句在腳本啓動後運行,將該檢查移至一個按下按鈕時調用的函數。 –

+0

謝謝你,你幫了很多 –

回答

0

我想你應該考慮使用一個類來代替的原始代碼,你將有一個很好的控制。 什麼辦法,我只是修正,而不是按鈕之前檢查它點擊獲取按鈕,點擊後的值,然後運行

後,你的代碼看起來是這樣的

from tkinter import * 
import tkinter.messagebox as box 

def dialog1(): 
    username=entry1.get() 
    password = entry2.get() 
    if (username == 'admin' and password == 'secret'): 
     box.showinfo('info','Correct Login') 
    else: 
     box.showinfo('info','Invalid Login') 


window = Tk() 
window.title('Countries Generation') 

frame = Frame(window) 

Label1 = Label(window,text = 'Username:') 
Label1.pack(padx=15,pady= 5) 

entry1 = Entry(window,bd =5) 
entry1.pack(padx=15, pady=5) 



Label2 = Label(window,text = 'Password: ') 
Label2.pack(padx = 15,pady=6) 

entry2 = Entry(window, bd=5) 
entry2.pack(padx = 15,pady=7) 




btn = Button(frame, text = 'Check Login',command = dialog1) 


btn.pack(side = RIGHT , padx =5) 
frame.pack(padx=100,pady = 19) 
window.mainloop() 

希望幫助你瞭解你的邏輯是什麼與以前的代碼錯誤

+0

我不使用班級,因爲這是上學,我們還沒有完成班級。非常感謝你,我終於明白我做錯了什麼,你一直很好 –