程序仅在存在未使用的窗口小部件时按预期工作

时间:2014-02-26 23:51:13

标签: python widget

所以,在我去爱好商店买它们之前,我一直在编写一个python脚本给我一些关于道具的统计数据。是的,它很适合实验,但我想知道我应该期待什么。

我遇到的问题是程序只在取消注释现已灭绝的wattEntry小部件时输出数据。我已经完成并评论了可能与之相关的所有内容,但每当我按下提交时,都没有任何反应。另一方面,仅取消注释小部件部分可以解决问题。我只想在Label小部件中输出,而不是条目。

from Tkinter import *
import numpy as np
from numpy import array
import  math

master = Tk()
master.title=('Static Thrust Calculator')
Equal = "Notes:"
thrustOutput=StringVar(master, 0)
maxRpmOutput=StringVar(master, 0)
reqWatts=StringVar(master, 0)

Watts="Watts Required:"
Pitch="Pitch(in):"
Diameter="Diameter(in):"
RPM="RPM:"
motorWatts="Max Motor watts:"
Temperature="Air Temperature(f):"
zero=0

#Submit Button Actions#############
def Submit():
    try:
        #wattEntry.delete(0,END)
        #wattEntry.insert(INSERT, zero)
        thrustOutput.set(zero)
        maxRpmOutput.set(zero)
        reqWatts.set(zero)


        pitchValue=np.fromstring(pitchEntry.get(), dtype=float, sep=' ')
        diameterValue=np.fromstring(diameterEntry.get(), dtype=float, sep=' ')
        rpmValue=np.fromstring(rpmEntry.get(), dtype=float, sep=' ')
        #wattValue=np.fromstring(wattEntry.get(), dtype=float, sep=' ')
        #motorValue=np.fromstring(wattEntry.get(), dtype=float, sep=' ')

        #Equation for watts required based on pitch, diameter, & RPM ##################################################################
        wattsReq=((diameterValue**4)*(rpmValue**3)*(pitchValue))/(1.888*10**14)
        #Max RPM of a propeller based on watts, diameter, & pitch #####################################################################
        maxRpm=((57367.7)*(motorValue**(1/3)))/((diameterValue**(4/3))*(pitchValue**(1/3)))*3
                #Static thrust generated based on RPM, diameter, & pitch ######################################################################
        staticThrust=((4.39299*(10**-8)*rpmValue)*(diameterValue**3.5)/(pitchValue**1/2)*(4.23333*10**-4)*rpmValue*pitchValue)*101.97
        #Prop RPM based on static thrust, pitch, diameter,
        ###############################################################################################################################
        reqWatts.set(wattsReq)
        thrustOutput.set(staticThrust)
        maxRpmOutput.set(maxRpm)
    except:
        print "error"

Label(master,text='Required Wattage Calculator').pack(pady=10)

#Watt Widgets
######################################################
wattLabel=Label(master, text=Watts)
wattLabel.pack(side=TOP)
wattEntry=Entry(master, text=Watts, width=10)
wattEntry.pack(side=TOP)
######################################################
#motor wattage Widgets
motorLabel=Label(master, text=motorWatts).pack(side=TOP)
motorEntry=Entry(master, text=motorWatts, width=10).pack(side=TOP)
#Diameter Widgets
diameterLabel=Label(master, text=Diameter)
diameterLabel.pack(side=TOP)
diameterEntry=Entry(master, text=Diameter, width=10)
diameterEntry.pack(side=TOP)
#Pitch Widgets
pitchLabel=Label(master, text=Pitch)
pitchLabel.pack(side=TOP)
pitchEntry=Entry(master, text=Pitch, width=10)
pitchEntry.pack(side=TOP)
#RPM Widgets
rpmLabel=Label(master, text=RPM)
rpmLabel.pack(side=TOP)
rpmEntry=Entry(master, text=RPM, width=10)
rpmEntry.pack(side=TOP)
#AirDensity Widgets
tempLabel=Label(master, text=Temperature).pack(side=TOP)
tempEntry=Entry(master, text=Temperature, width=10).pack(side=TOP)


submitButton=Button(master, text="Submit", width=5, command=Submit).pack(side=TOP)

thrustLabel=Label(master, text="Static Thrust(g):").pack(side=TOP)
thrustBox=Label(master, textvariable=thrustOutput).pack(side=TOP)

maxRpmLabel=Label(master, text="MaxRPM").pack(side=TOP)
maxRpmBox=Label(master, textvariable=maxRpmOutput).pack(side=TOP)
wattsLabel=Label(master, text=Watts).pack(side=TOP)
wattsBox=Label(master, textvariable=reqWatts).pack(side=TOP)


mainloop()

每当

wattLabel=Label(master, text=Watts)
wattLabel.pack(side=TOP)
wattEntry=Entry(master, text=Watts, width=10)
wattEntry.pack(side=TOP)

已删除,按下提交时它不会执行任何操作。将其添加回来可以解决问题。 有没有什么可以捕获会导致这种行为? 我没有在IDLE中收到任何错误,也没有任何内容被打印或返回到控制台。

这些是正在使用的小部件的唯一其他实例:

#Submit Button Actions#############
def Submit():
    try:
        #wattEntry.delete(0,END)
        #wattEntry.insert(INSERT, zero)
        thrustOutput.set(zero)
        maxRpmOutput.set(zero)
        reqWatts.set(zero)

- 这个不会影响性能

        #wattValue=np.fromstring(wattEntry.get(), dtype=float, sep=' ')
        #motorValue=np.fromstring(wattEntry.get(), dtype=float, sep=' ')

- 这也不是

但取消注释小部件区域确实..不确定发生了什么。

1 个答案:

答案 0 :(得分:0)

#Submit Button Actions#############
def Submit():
    try:
        #wattEntry.delete(0,END)
        #wattEntry.insert(INSERT, zero)
        thrustOutput.set(zero)
        maxRpmOutput.set(zero)
        reqWatts.set(zero)


        pitchValue=np.fromstring(pitchEntry.get(), dtype=float, sep=' ')
        diameterValue=np.fromstring(diameterEntry.get(), dtype=float, sep=' ')
        rpmValue=np.fromstring(rpmEntry.get(), dtype=float, sep=' ')
        #wattValue=np.fromstring(wattEntry.get(), dtype=float, sep=' ')
        motorValue=np.fromstring(wattEntry.get(), dtype=float, sep=' ')

        #Equation for watts required based on pitch, diameter, & RPM ##################################################################
        wattsReq=((diameterValue**4)*(rpmValue**3)*(pitchValue))/(1.888*10**14)
        #Max RPM of a propeller based on watts, diameter, & pitch #####################################################################
        maxRpm=((57367.7)*(motorValue**(1/3)))/((diameterValue**(4/3))*(pitchValue**(1/3)))*3
                #Static thrust generated based on RPM, diameter, & pitch ######################################################################
        staticThrust=((4.39299*(10**-8)*rpmValue)*(diameterValue**3.5)/(pitchValue**1/2)*(4.23333*10**-4)*rpmValue*pitchValue)*101.97
        #Prop RPM based on static thrust, pitch, diameter,
        ###############################################################################################################################
        reqWatts.set(wattsReq)
        thrustOutput.set(staticThrust)
        maxRpmOutput.set(maxRpm)
    except:
        print "error"
        raise

现在您将看到为什么会收到错误并立即找到解决方案; P

(很多这些计算需要瓦特(您需要更改计算或将瓦特设置为静态值))