[英]How can I make two different events trigger the same action with Tkinter?
As a Python beginner I've been trying to write a simple login module that can be utilized by other Python programs I've written in order to restrict who can open the program.作为一名 Python 初学者,我一直在尝试编写一个简单的登录模块,我编写的其他 Python 程序可以使用该模块来限制谁可以打开该程序。 I'm only writing this with the purpose of learning and not actually using it but I need some help.
我写这个只是为了学习而不是实际使用它,但我需要一些帮助。
My issue is that once the credentials are entered, I can only make my Enter key OR my Mouse-One trigger the "login" function.我的问题是,一旦输入凭据,我就只能让 Enter 键或 Mouse-One 触发“登录”功能。 I'm a little stuck trying to figure out how to make both Enter AND Mouse click valid events to trigger the function.
我有点想弄清楚如何使 Enter 和 Mouse 单击有效事件来触发该功能。
Here is what I have so far.这是我到目前为止所拥有的。 Currently only the Enter key will trigger "login":
目前只有 Enter 键会触发“登录”:
from tkinter import *
def mainlogin():
# Root
root = Tk()
root.title("Login")
root.geometry("240x110")
root.protocol("WM_DELETE_WINDOW", disable_event)
# Globals
global allow_entry
allow_entry = False
# Login Credentials
my_username = 'abcd'
my_password = '1234'
# Login Function
def login(event):
if my_username == username.get() and my_password == pswd.get():
allow_entry = True
root.quit()
else:
allow_entry = False
wrong_input = Label(root, text="Wrong username or password!")
wrong_input.pack(padx=5, pady=5)
# Enter Key Bind
root.bind("<Return>", login)
# Login entry widgets
username = Entry(root)
username.pack(padx=5, pady=5)
pswd = Entry(root, show="*")
pswd.pack(padx=5, pady=5)
# Buttons
login_button = Button(root, text="Login", command=login)
login_button.pack()
### MAIN LOOP ###
root.mainloop()
def quit_login():
allow_entry = False
root.quit()
def disable_event():
pass
### NAME == MAIN ###
if __name__ == "__main__":
mainlogin()
You can set a default argument for the mouse button event:您可以为鼠标按钮事件设置默认参数:
def login(event=None):
if not event:
print('>> Button')
else:
print('>> Return')
......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.