【问题标题】:How to block typing in Entry while still keeping its functionality?如何在保持其功能的同时阻止输入 Entry?
【发布时间】:2018-12-19 23:44:24
【问题描述】:

我正在使用 Tkinter 制作计算器。我希望能够阻止用户直接在条目中输入,但我仍然不想简单地完全停用条目,因为我需要来自按钮的输入命令才能使其工作。

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()

【问题讨论】:

  • 为什么不用Label 而不是Entry

标签: python python-2.7 tkinter


【解决方案1】:

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

the docs:

状态=

进入状态:NORMALDISABLED或“只读”(同DISABLED,但内容仍可以选择和复制) .默认为正常。请注意,如果您将此设置为 DISABLED 或“只读”,则会忽略对 insertdelete 的调用。 (州/州)

【讨论】:

    猜你喜欢
    • 2018-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多