简体   繁体   English

如何使用 Tkinter 使两个不同的事件触发相同的操作?

[英]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.

相关问题 如何使同一列上的两个小部件在 Tkinter 中具有不同的宽度? - How can I make two widgets on the same column have different widths in Tkinter? 如何让我的机器人在使用不同的消息时执行相同的操作? - How can I make my bot execute the same action while using different messages? 如何将函数分配给 Tkinter 中具有两个不同变量的按钮? - How can I assign a function to a button in Tkinter with two different variables? 从Python中的另一个类触发Tkinter事件 - Trigger Tkinter events from a different class in Python 如何触发和侦听python中的事件? - How can I trigger and listen for events in python? 如何在 tkinter 中对两个同时的按键事件进行编程,以使用按键事件字典对角移动画布项目? - How can I program two simultaneous key press events in tkinter to move a canvas item diagonally using a dictionary of keypress events? 如何使同一事件对 tkinter 中的不同功能具有不同的含义? - How to make same event mean different things for different functions in tkinter? 如何使两台印刷机在 tkinter 中制作相同的 output - How to make two presses make the same output in tkinter 如何在Tkinter中同时运行两个图像循环 - How do I make two image loops run at the same time in Tkinter 如何制作一个按钮,允许我将两个变量发送到 Tkinter 中的同一个函数中? - How do I make a button that allows me to send two variables into the same function in Tkinter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM