|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
刚学了tkinter 学着做了个简单的计算器,然后想用pyinstaller 打包成exe文件,放在别的电脑上用,结果报错
错误如下:
Exception in Tkinter callback
Traceback (most recent call last):
File "tkinter\__init__.py", line 1705, in __call__
File "fluid_column_pressure.py", line 38, in calc
ValueError: could not convert string to float:
这是代码错误吗?,单独运行一切正常啊,可是从主菜单点按钮 弹出这个窗口的时候,就报上面的错误
from tkinter import *
def main():
root = Tk()
root.title("液柱压力计算")
frame = Frame(root)
frame.pack(padx=20,pady=20)
v1 =StringVar()
v2 =StringVar()
v3 =StringVar()
v4 =StringVar()
def test(content):
if not content:
return True
try:
float(content)
return True
except ValueError:
return False
testCMD =root.register(test)
Label(frame,text="液柱垂深(m):",font=("宋体",20)).grid(row=0,column=0)
Entry(frame,textvariable=v1,width=20,validate="key",validatecommand=(testCMD,'%P')).grid(row=0,column=1)
Label(frame,text="液体密度(g/cm3):",font=("宋体",20)).grid(row=1,column=0)
Entry(frame,textvariable=v2,width=20,validate="key",validatecommand=(testCMD,'%P')).grid(row=1,column=1)
Label(frame,text="液柱压力(MPa):",font=("宋体",20)).grid(row=3,column=0)
Entry(frame,textvariable=v3,width=20,validate="key",validatecommand=(testCMD,'%P')).grid(row=3,column=1)
Label(frame,text="液柱压力(psi):",font=("宋体",20)).grid(row=4,column=0)
Entry(frame,textvariable=v4,width=20,validate="key",validatecommand=(testCMD,'%P')).grid(row=4,column=1)
def calc():
result1 = float(v1.get()) * float(v2.get())*9.8/1000
v3.set(float(result1))
v4.set(result1*145.037)
def test_empty():
v1.set("")
v2.set("")
v3.set("")
v4.set("")
Button(frame,text="计算结果",command=calc,font=("宋体",14),fg="red").grid(row=2,column=1,pady=5)
Button(frame,text="重 置",command=test_empty,font=("宋体",14),fg="red").grid(row=2,column=0,pady=5)
mainloop()
if __name__=='__main__':
main()
因为你创建了多个根窗口,一个程序只能有一个根窗口,导致你对 v 设置时候都获取是对另一个窗口的值
但是另一个窗口又没有值,所以这里默认都是空字符串'' 导致转化为 float报错
还有就是 mainloop() 也只需要一个,多余的都是没有用的,参考代码:
from tkinter import *
master = Tk()
frame = Frame(width=1000,height=1000,bd=1,relief=SUNKEN)
frame.pack(padx=20,pady=20)
master.title("石油工程计算小助手")
def callback1():
root = Toplevel()
root.title("液柱压力计算")
frame = Frame(root)
frame.pack(padx=20,pady=20)
v1 =StringVar()
v2 =StringVar()
v3 =StringVar()
v4 =StringVar()
def test(content):
if not content:
return True
try:
float(content)
return True
except ValueError:
return False
testCMD =root.register(test)
Label(frame,text="液柱垂深(m):",font=("宋体",20)).grid(row=0,column=0)
Entry(frame,textvariable=v1,width=20,validate="key",validatecommand=(testCMD,'%P')).grid(row=0,column=1)
Label(frame,text="液体密度(g/cm3):",font=("宋体",20)).grid(row=1,column=0)
Entry(frame,textvariable=v2,width=20,validate="key",validatecommand=(testCMD,'%P')).grid(row=1,column=1)
Label(frame,text="液柱压力(MPa):",font=("宋体",20)).grid(row=3,column=0)
Entry(frame,textvariable=v3,width=20,validate="key",validatecommand=(testCMD,'%P')).grid(row=3,column=1)
Label(frame,text="液柱压力(psi):",font=("宋体",20)).grid(row=4,column=0)
Entry(frame,textvariable=v4,width=20,validate="key",validatecommand=(testCMD,'%P')).grid(row=4,column=1)
def calc():
result1 = float(v1.get()) * float(v2.get())*9.8/1000
v3.set(float(result1))
v4.set(result1*145.037)
def test_empty():
v1.set("")
v2.set("")
v3.set("")
v4.set("")
Button(frame,text="计算结果",command=calc,font=("宋体",14),fg="red").grid(row=2,column=1,pady=5)
Button(frame,text="重 置",command=test_empty,font=("宋体",14),fg="red").grid(row=2,column=0,pady=5)
def callback2():
root = Toplevel()
root.title("水泥浆密度配浆计算")
frame1 = Frame(root)
frame1.pack(padx=20,pady=20)
frame2 = Frame(root)
frame2.pack(padx=20,pady=20)
frame3 = Frame(root)
frame3.pack(padx=10,pady=20)
v1 = StringVar()
v2 = StringVar()
v3 = StringVar()
v4 = StringVar()
v5 = StringVar()
v6 = StringVar()
v7 = StringVar()
def test(content):
if not content:
return True
try:
float(content)
return True
except ValueError:
return False
testCMD =root.register(test)
Label(frame1, text="请输入参数", font=("宋体", 10),fg="gray").grid(sticky=W)
Label(frame1,text="水泥浆密度(g/cm3):",font=("宋体",20)).grid(row=1,column=0,sticky=W)
Entry(frame1,textvariable=v1,width=20,validate="key",validatecommand=(testCMD,'%P')).grid(row=1,column=1)
Label(frame1,text="水泥密度(g/cm3):",font=("宋体",20)).grid(row=2,column=0,sticky=W)
Entry(frame1,textvariable=v2,width=20,validate="key",validatecommand=(testCMD,'%P')).grid(row=2,column=1)
Label(frame1,text="配浆水密度(g/cm3):",font=("宋体",20)).grid(row=3,column=0,sticky=W)
Entry(frame1,textvariable=v3,width=20,validate="key",validatecommand=(testCMD,'%P')).grid(row=3,column=1)
Label(frame1,text="配浆量(m3):",font=("宋体",20)).grid(row=4,column=0,sticky=W)
Entry(frame1,textvariable=v4,width=20,validate="key",validatecommand=(testCMD,'%P')).grid(row=4,column=1)
def calc():
result1 = float(v4.get()) * float(v2.get())*(float(v1.get()) - float(v3.get()))/(float(v2.get()) - float(v3.get()))
v5.set(float(result1)*1000)
result2 = 1.465*(1-0.317*(float(v1.get()))) * float(v4.get())
v6.set(result2)
v7.set(100 * float(result2) / float(result1))
def test_empty():
v1.set("")
v2.set("")
v3.set("")
v4.set("")
v5.set("")
v6.set("")
v7.set("")
Button(frame2,text="计算结果",command=calc,font=("宋体",14),fg="red").grid(row=4,column=1,pady=5)
Button(frame2,text="重 置",command=test_empty,font=("宋体",14),fg="red").grid(row=4,column=0,pady=5)
Label(frame3, text="计算结果", font=("宋体", 10),fg="gray").grid(sticky=W)
Label(frame3,text="所需水泥量(kg):",font=("宋体",20)).grid(row=5,column=0,sticky=W)
Entry(frame3,textvariable=v5,width=20,validate="key",validatecommand=(testCMD,'%P')).grid(row=5,column=1,sticky=W)
Label(frame3,text="所需水量(m3):",font=("宋体",20)).grid(row=6,column=0,sticky=W)
Entry(frame3,textvariable=v6,width=20,validate="key",validatecommand=(testCMD,'%P')).grid(row=6,column=1)
Label(frame3,text="水灰比(%):",font=("宋体",20)).grid(row=7,column=0,sticky=W)
Entry(frame3,textvariable=v7,width=20,validate="key",validatecommand=(testCMD,'%P')).grid(row=7,column=1)
b = Button(frame, text="液柱压力计算",font=("宋体",20),command=callback1)
c = Button(frame, text="水泥浆密度配浆计算",font=("宋体",20),command=callback2)
b.pack(anchor=W)
c.pack()
mainloop()
|
|