如何在保持其功能的同时阻止Entry中的键入?

时间:2018-12-19 23:44:24

标签: python python-2.7 tkinter

我正在使用Tkinter制作计算器。我希望能够阻止用户直接在Entry中键入内容,但是我仍然不想完全取消激活Entry,因为我需要按钮上的输入命令才能使它起作用。

from Tkinter import *
root = Tk()
root.title("Calculadora")
root.resizable(height = False, width = False)

#functions------------------------------------------------------------------

display_input = StringVar()

number_storage = ""

def button_click(buttons):
    global number_storage
    number_storage = number_storage + str(buttons)
    display_input.set(number_storage)


#row 1---------------------------------------------------------------------------

display = Entry(root, textvariable = display_input, justify = 'right', font = ("Simplified Arabian Fixed", 18), bg = "black", fg = "white", bd = 25).grid(columnspan = 4)

Button7 = Button(root, command = lambda: button_click(7), bd = 10, text= "7", padx = 9, font = ("Simplified Arabian Fixed", 15), bg = "black", fg = "white").grid(column = 0, row = 1, sticky = 'ew')

Button8 = Button(root, command = lambda: button_click(8), bd = 10, text = "8", padx = 9, font = ("Simplified Arabian Fixed", 15), bg = "black", fg = "white").grid(column = 1, row = 1, sticky = 'ew')

Button9 =  Button(root, command = lambda: button_click(9), bd = 10, text = "9", padx = 9, font = ("Simplified Arabian Fixed", 15), bg = "black", fg = "white").grid(column = 2, row = 1, sticky = 'ew')

Division = Button(root, command = lambda: button_click("/"), bd = 10, text = "/", padx = 9, font = ("Simplified Arabian Fixed", 15), bg = "grey30", fg = "white").grid(column = 3, row = 1, sticky = 'ew')

root.mainloop()

1 个答案:

答案 0 :(得分:1)

构造state="readonly"时通过Entry;它仍然允许选择文本,并且以编程方式更改display_input仍然可以更改显示,但是不允许直接用户输入。

the docs

  

state =

     
    

输入状态:正常已禁用或“只读”(与已禁用相同,但仍可以选择和复制内容) 。默认值为 NORMAL 。请注意,如果将此设置为 DISABLED 或“只读”,则对insertdelete的调用将被忽略。 (州/州)