【问题标题】:How to make Entry Boxes interact w/ each other in Python IDLE Tkinter?如何使输入框在 Python IDLE Tkinter 中相互交互?
【发布时间】:2014-05-12 20:55:03
【问题描述】:

我正在尝试制作一个基本的面积计算器来做这个 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 我该怎么做?

【问题讨论】:

  • boxBaseboxHeight 是小部件的实例。使用.get() 方法返回它们的值。 effbot.org/tkinterbook/entry.htm#Tkinter.Entry.get-method
  • 嗯,每当我尝试使用 .get() 时,例如我的 boxBase,它说“boxBase”未定义?有什么想法吗?
  • 发布你的代码和完整的错误,我会告诉你,否则可能是很多事情。

标签: python user-interface tkinter python-idle


【解决方案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()

【讨论】:

  • 谢谢,我会的,似乎每当我发布某些内容时,人们总是不赞成或试图关闭它?哈哈,谢谢,尽管男人
  • 呵呵,是的,我看到了很多.. 反正 np 人。只是问我是否需要帮助。我在 python 中做了很多计算应用程序。
  • 嗯,我一直收到错误消息“无法将字符串转换为浮点数”?有什么想法吗?
  • 你输入的是 ,而不是 .也许? :) 像 1,5 应该是 1.5
  • 我编辑了代码。将 StringVar() 更改为 DoubleVar()。并删除了浮动转换。那你就不用考虑这个了。但是,如果您输入文本,它仍然会引发错误。
猜你喜欢
  • 1970-01-01
  • 2014-08-12
  • 2021-11-07
  • 2013-06-19
  • 2014-08-30
  • 2022-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多