【发布时间】:2016-08-04 08:29:08
【问题描述】:
我制作了一个简单的计算器,带有 1、2、3 等按钮......但我无法将 1 或 2 等按钮文本放到计算器屏幕上。 如果你们给我一些提示会很有帮助..
from tkinter import *
root = Tk()
buttons = '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '+', '-'
def calc(value):
res = num.get()
res = res + " " + value
res = str(eval(res))
num.set(res)
num = StringVar()
rows = 1
col = 0
ent = Entry(root, textvariable = num, background = "#D0F3F5", border = 2, width = 50)
ent.bind('<FocusOut>')
ent.grid(row = 0 , column = 0, columnspan = 4, ipadx = 5, ipady = 5)
Button(root, text = '=', width = 45, command = calc).grid(column = 0, row = 5, columnspan = 4)
for button in buttons:
button = Button(root,width = 10, text = button, command = lambda: calc(button))
#button.bind('<Button-1>', lambda e: screen(button))
button.grid(row = rows, column = col, sticky = "W E")
button['relief']="groove"
col = col + 1
if col == 4:
rows = rows + 1
col = 0
if rows > 6:
break
root.mainloop()
【问题讨论】:
-
无法理解某些位的用途。
command = calc这怎么用?for button in buttons: button = Button(..)在这里使用相同的命名。res + " " + valuestr(eval(res))你想评估什么?