如何禁用Tkinter Entry组件的键盘输入,仅通过按钮操作?
问题描述
我编写了一段Tkinter代码,创建了Entry输入框和数字操作按钮,希望仅通过按钮向Entry输入内容,禁止通过键盘输入,但找不到实现方法。以下是我的代码:
import tkinter as tk from tkinter import * root = tk.Tk() yazı = ' ' root.state('zoomed') root.resizable(False, False) root.configure(bg='gray95') labelsayfa = Entry(root, text='', width=28, font='Calibri 15', bg='dark gray') c1 = Canvas(width=700, height=650, bg='dark gray') c1.place(x=415, y=60) def b1g(): labelsayfa.insert(len(str(labelsayfa)), 1) def b2g(): labelsayfa.insert(len(str(labelsayfa)), 2) def b3g(): labelsayfa.insert(len(str(labelsayfa)), 3) def b4g(): labelsayfa.insert(len(str(labelsayfa)), 4) def b5g(): labelsayfa.insert(len(str(labelsayfa)), 5) def b6g(): labelsayfa.insert(len(str(labelsayfa)), 6) def b7g(): labelsayfa.insert(len(str(labelsayfa)), 7) def b8g(): labelsayfa.insert(len(str(labelsayfa)), 8) def b9g(): labelsayfa.insert(len(str(labelsayfa)), 9) def b0g(): labelsayfa.insert(len(str(labelsayfa)), 10) def bacg(): labelsayfa.delete(0, END) def bsilg(): labelsayfa.delete(len(labelsayfa.get()) - 1, END) def bokg(): pass b1 = tk.Button(root, text='1', width=10, height=5, command=b1g) b2 = tk.Button(root, text='2', width=10, height=5, command=b2g) b3 = tk.Button(root, text='3', width=10, height=5, command=b3g) b4 = tk.Button(root, text='4', width=10, height=5, command=b4g) b5 = tk.Button(root, text='5', width=10, height=5, command=b5g) b6 = tk.Button(root, text='6', width=10, height=5, command=b6g) b7 = tk.Button(root, text='7', width=10, height=5, command=b7g) b8 = tk.Button(root, text='8', width=10, height=5, command=b8g) b9 = tk.Button(root, text='9', width=10, height=5, command=b9g) b0 = tk.Button(root, text='0', width=10, height=5, command=b0g) bac = tk.Button(root, text='AC', width=10, height=5, command=bacg) bsil = tk.Button(root, text='<--', width=10, height=5, command=bsilg) bok = tk.Button(root, text='✓', width=39, height=2, command=bokg) def tag(): c1.place_forget() labelsayfa.place(x=1200, y=100) bac.place(x=1200, y=160) b1.place(x=1300, y=160) b2.place(x=1400, y=160) b3.place(x=1200, y=260) b4.place(x=1300, y=260) b5.place(x=1400, y=260) b6.place(x=1200, y=360) b7.place(x=1300, y=360) b8.place(x=1400, y=360) b9.place(x=1200, y=460) b0.place(x=1300, y=460) bsil.place(x=1400, y=460) bok.place(x=1200, y=560) tag() root.mainloop()
解决方案
有两种简洁有效的方法实现仅允许按钮操作Entry、禁止键盘输入的需求:
方法1:绑定键盘事件拦截输入
给Entry组件绑定<Key>事件,让事件处理函数返回False,直接阻断所有键盘输入动作,同时不影响按钮通过代码修改Entry内容:
# 在创建labelsayfa后添加这行代码 labelsayfa.bind("<Key>", lambda e: False)
方法2:使用验证函数精准控制
通过Entry的validate和validatecommand选项,设置仅允许程序内部修改(比如按钮调用insert/delete),拒绝用户键盘输入:
# 先定义验证函数 def validate_input(action): # action为'0'表示程序内部修改,'1'表示用户键盘输入 return action == '0' # 创建Entry时配置验证规则 labelsayfa = Entry(root, text='', width=28, font='Calibri 15', bg='dark gray', validate='key', validatecommand=(root.register(validate_input), '%d'))
额外代码修正
原代码存在两处小问题,建议一并修正:
b0g函数插入的是10,但按钮显示0,应改为插入0- 插入内容时用
END替代len(str(labelsayfa)),更简洁准确
修正后的完整代码
import tkinter as tk from tkinter import * root = tk.Tk() yazı = ' ' root.state('zoomed') root.resizable(False, False) root.configure(bg='gray95') # 方法1:绑定键盘事件拦截输入(二选一即可) labelsayfa = Entry(root, text='', width=28, font='Calibri 15', bg='dark gray') labelsayfa.bind("<Key>", lambda e: False) # # 方法2:使用验证函数(二选一即可) # def validate_input(action): # return action == '0' # labelsayfa = Entry(root, text='', width=28, font='Calibri 15', bg='dark gray', # validate='key', validatecommand=(root.register(validate_input), '%d')) c1 = Canvas(width=700, height=650, bg='dark gray') c1.place(x=415, y=60) def b1g(): labelsayfa.insert(END, 1) def b2g(): labelsayfa.insert(END, 2) def b3g(): labelsayfa.insert(END, 3) def b4g(): labelsayfa.insert(END, 4) def b5g(): labelsayfa.insert(END, 5) def b6g(): labelsayfa.insert(END, 6) def b7g(): labelsayfa.insert(END, 7) def b8g(): labelsayfa.insert(END, 8) def b9g(): labelsayfa.insert(END, 9) def b0g(): labelsayfa.insert(END, 0) # 修正:插入0而非10 def bacg(): labelsayfa.delete(0, END) def bsilg(): # 增加判断,避免空内容时报错 if len(labelsayfa.get()) > 0: labelsayfa.delete(len(labelsayfa.get()) - 1, END) def bokg(): pass b1 = tk.Button(root, text='1', width=10, height=5, command=b1g) b2 = tk.Button(root, text='2', width=10, height=5, command=b2g) b3 = tk.Button(root, text='3', width=10, height=5, command=b3g) b4 = tk.Button(root, text='4', width=10, height=5, command=b4g) b5 = tk.Button(root, text='5', width=10, height=5, command=b5g) b6 = tk.Button(root, text='6', width=10, height=5, command=b6g) b7 = tk.Button(root, text='7', width=10, height=5, command=b7g) b8 = tk.Button(root, text='8', width=10, height=5, command=b8g) b9 = tk.Button(root, text='9', width=10, height=5, command=b9g) b0 = tk.Button(root, text='0', width=10, height=5, command=b0g) bac = tk.Button(root, text='AC', width=10, height=5, command=bacg) bsil = tk.Button(root, text='<--', width=10, height=5, command=bsilg) bok = tk.Button(root, text='✓', width=39, height=2, command=bokg) def tag(): c1.place_forget() labelsayfa.place(x=1200, y=100) bac.place(x=1200, y=160) b1.place(x=1300, y=160) b2.place(x=1400, y=160) b3.place(x=1200, y=260) b4.place(x=1300, y=260) b5.place(x=1400, y=260) b6.place(x=1200, y=360) b7.place(x=1300, y=360) b8.place(x=1400, y=360) b9.place(x=1200, y=460) b0.place(x=1300, y=460) bsil.place(x=1400, y=460) bok.place(x=1200, y=560) tag() root.mainloop()
内容的提问来源于stack exchange,提问作者Mehmet Kılınçkaya
相关产品推荐
相关产品推荐

