【发布时间】:2021-06-12 03:01:01
【问题描述】:
我正在开发一个 GUI 计算器,但是我无法在输入框中打印数字的值。请帮我?拜托,我才 15 岁,所以,请保持我的代码水平。
from tkinter import *
root=Tk()
root.title("Calculator")
#Console from where the input is to be taken
e=Entry(root, width="25", borderwidth="2", )
e.grid(row=0, column=0, columnspan="4", padx=10, pady=10)
#Defining Buttons
button1=Button(root, text="7", padx="20", pady="10", fg="white",bg="black", command=lambda:button_add(1))
button2=Button(root, text="8", padx="20", pady="10", fg="white",bg="black", command=lambda:button_add(2))
button3=Button(root, text="9", padx="20", pady="10", fg="white",bg="black", command=lambda:button_add(3))
button4=Button(root, text="4", padx="20", pady="10", fg="white",bg="black", command=lambda: button_add(4))
button5=Button(root, text="5", padx="20", pady="10", fg="white",bg="black", command=lambda: button_add(5))
button6=Button(root, text="6", padx="20", pady="10", fg="white",bg="black", command=lambda: button_add(6))
button7=Button(root, text="1", padx="20", pady="10", fg="white",bg="black", command=lambda: button_add(7))
button8=Button(root, text="2", padx="20", pady="10", fg="white",bg="black", command=lambda: button_add(8))
button9=Button(root, text="3", padx="20", pady="10", fg="white",bg="black", command=lambda: button_add(9))
button0=Button(root, text="0", width="9", padx="20", pady="10", fg="white",bg="black", command=lambda: button_add(0))
operation1=Button(root, text="÷", padx="20", pady="10", command=lambda: button_add)
operation2=Button(root, text="x", padx="20", pady="10", command=lambda: button_add)
operation3=Button(root, text="-", padx="21", pady="11", command=lambda: button_add)
operation4=Button(root, text="+", padx="20", pady="10", command=lambda: button_add)
operation5=Button(root, text="=", padx="20", pady="10", bg="yellow", command=lambda: button_add)
clear=Button(root, text="CE", padx="20", pady="10", width="8", command=lambda: button_add)
backspace=Button(root, text="<--", padx="20", pady="10", width="8", command=lambda: button_add)
#Showing Buttons
button1.grid(row="2", column="0")
button2.grid(row="2", column="1")
button3.grid(row="2", column="2")
button4.grid(row="3", column="0")
button5.grid(row="3", column="1")
button6.grid(row="3", column="2")
button7.grid(row="4", column="0")
button8.grid(row="4", column="1")
button9.grid(row="4", column="2")
button0.grid(rows="5", column="0", columnspan="2")
operation1.grid(row="2", column="3")
operation2.grid(row="3", column="3")
operation3.grid(row="4", column="3")
operation4.grid(row="5", column="3")
operation5.grid(row="5", column="2")
clear.grid(row="1", column="0", columnspan="2")
backspace.grid(row="1", column="2", columnspan="2")
def button_add():
e.delete(0, END)
e.insert(0, number)
return
root.mainloop()
#Show the buttons______X
#Buttons should work
#Get input
#Input should be seen
#Process the information
#Show the output
【问题讨论】:
-
您正在将数字传递给您的
button_add函数,但您没有接收它的参数。您应该将number作为参数。您需要将其转换为字符串以将其存储在文本框中 -
您应该传递类似
command=lambda: button_add(<number>)的命令并添加类似def button_add(number):的number参数。此外,您需要在顶部定义函数以防止任何错误