如何让Python IDLE Tkinter中的Entry Box互相交互?

时间:2014-05-12 20:55:03

标签: python user-interface tkinter python-idle

我正在尝试制作一个基本区域计算器来执行此基础* 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并按boxSolution时打印到buttonCalculate我该怎么做?

1 个答案:

答案 0 :(得分:1)

试试这个: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()