我想做一个基本的面积计算器,做这个base*height=area
到目前为止,我的代码是这样的(至少是你需要看到的部分)#Labels and Boxes
def calculate():
boxBase*boxHeight
buttonCalculate = Button(text = "Calculate area...", command = calculate)
buttonCalculate.grid(row =4, column =0)
myLabel3=Label(text="Area", fg="black")
myLabel3.grid(row =3, column =0, sticky='w')
boxSolution= Entry()
boxSolution.grid(row =3, column =1)
myLabel2=Label(text="Base", fg="black")
myLabel2.grid(row =0, column =0, sticky='w')
boxBase= Entry()
boxBase.grid(row =0, column =1)
myLabel=Label(text="Height", fg="black",)
myLabel.grid(row =1, column =0, sticky = "w")
boxHeight= Entry()
boxHeight.grid(row =1, column =1)
我想让boxBase
乘以boxHeight
,当我按下buttonCalculate
时,打印成boxSolution
我怎么做呢?
试试这个:Python 2.7
#Labels and Boxes
from Tkinter import *
WD = Tk()
WD.geometry('350x250+50+200')
WD.title("Area")
solution = DoubleVar()
base = DoubleVar()
height = DoubleVar()
def calculate():
boxSolution.delete(0,END)
boxSolution.insert(0,(base.get())*(height.get()))
buttonCalculate = Button(WD,text = "Calculate area...", command = calculate)
buttonCalculate.grid(row =4, column =0)
myLabel3=Label(WD,text="Area", fg="black")
myLabel3.grid(row =3, column =0, sticky='w')
boxSolution= Entry(WD,textvariable=solution)
boxSolution.grid(row =3, column =1)
myLabel2=Label(WD,text="Base", fg="black")
myLabel2.grid(row =0, column =0, sticky='w')
boxBase= Entry(WD,textvariable=base)
boxBase.grid(row =0, column =1)
myLabel=Label(WD,text="Height", fg="black",)
myLabel.grid(row =1, column =0, sticky = "w")
boxHeight= Entry(WD,textvariable=height)
boxHeight.grid(row =1, column =1)
WD.mainloop()