【发布时间】: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