首页 > 解决方案 > 在输入框上显示按钮的值

问题描述

我正在开发一个 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

标签: pythontkinterlambda

解决方案


要在条目中打印文本,请在 button_add 函数中写入 -

def button_add(num):
    e.insert('end',num)# e is the Entry

推荐阅读