簡體   English   中英

輸入密鑰Python Tkinter

[英]Enter key Python Tkinter

嗨,基本上,我希望我的程序在每次按下Enter鍵時都能“登錄”。 我該怎么做?

我的代碼:

def Login(event=None):
Db() #calls the database function/ subroutine
if USERNAME.get() == "" or PASSWORD.get() == "":
    lbl_text.config(text="Please fill in the required details", fg="red", font="Arial")
    PASSWORD.set("") #resets the password field to nill (with no username the password field is useless)
else:
    cursor.execute("SELECT * FROM `member` WHERE `username` = ? AND `password` = ?", (USERNAME.get(), PASSWORD.get())) #username and password retrieved from database
    if cursor.fetchone() is not None:
        Home()
        PASSWORD.set("") #resets password field to nill, so nobody can log in with same credentials if the log in window is left open
        lbl_text.config(text="")
    else:
        lbl_text.config(text="Incorrect Details entered", fg="red", font="Arial")
        PASSWORD.set("") #resets the password field to nill (I don't do the same with the password field, as the password field is typically wrong)   
cursor.close()
conn.close()

btn_login = Button(Form, text="Login", width=45, command=Login)
btn_login.grid(pady=25, row=3, columnspan=3)
btn_login.bind('<Return>', Login)

謝謝

您可以在tkinter中將enter綁定到一個函數:

from tkinter import *

window = Tk()
window.geometry("600x400")
window.title("Test")

def test(event):
    print("Hi")

window.bind("<Return>", test)

window.mainloop()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM