gpt4 book ai didi

python - 如何提示用户大写锁定已启用

转载 作者:行者123 更新时间:2023-11-30 22:24:41 28 4
gpt4 key购买 nike

当我将光标放在条目小部件中时,我试图检测大写锁定是否打开,但不知道如何处理。

我在网站上找到了这些答案,但没有一个满足我的需求:caps locks ans shift key statuscurrent key lock status

from tkinter import *

root = Tk()
root.geometry("400x400")

e1 = Entry(root, width=40)
e1.focus()
e1.pack()

e2 = Entry(root, width=40)
e2.place(x=70, y=100)

root.mainloop()

description of cursor in entry widget

欢迎您就如何执行此操作提出建议。

最佳答案

您可以使用条目上的绑定(bind)来检测用户是否在使用大写锁定的情况下进行输入。事件修饰符Lock使您能够仅在大写锁定打开时触发事件。因此,将您的警告绑定(bind)到 '<Lock-KeyPress>' ,每次用户在大写锁定打开时按下按键时都会显示该信息。如果你想让警告只显示一次,只需在with_caps_lock中解绑事件即可。 .

这是一个例子:

import tkinter as tk


def with_caps_lock(event):
if event.keysym != "Caps_Lock":
# this if statetement prevent the warning to show up when the user
# switches off caps lock
print('WARNING! Caps Lock is on.')
# unbind to do it only once
e1.unbind('<Lock-KeyPress>', bind_id)


root = tk.Tk()
root.geometry("400x400")

e1 = tk.Entry(root, width=40)
e1.focus()
e1.pack()
# show warning when the user types with caps lock on
bind_id = e1.bind('<Lock-KeyPress>', with_caps_lock)

root.mainloop()

关于python - 如何提示用户大写锁定已启用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47772051/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com