m0_64997461 2022-06-02 19:23 采纳率: 100%
浏览 58
已结题

python tkinter代码提问

请问大家这里如何将字符串类型转为浮点型进行数学计算而不被报错?

img


import tkinter as tk
from tkinter import *
from tkinter import messagebox
root=tk.Tk()
root.geometry('+700+200')
root.title('登录')
name=tk.StringVar()
password=tk.StringVar()
Label(root,text='用户名',width=6).place(x=1,y=21)
Entry(root,width=20,textvariable=name).place(x=45,y=21)
Label(root,text='密码',width=6).place(x=1,y=55)
Entry(root,width=20,show='*',textvariable=password).place(x=45,y=55)
def then():
import tkinter as tk
from tkinter import messagebox
root=tk.Tk()
root.geometry('+700+200')
root.title('BMI')
shengao=tk.StringVar()
tizhong=tk.StringVar()
Label(root,text='身高(m)',width=6).place(x=1,y=21)
Entry(root,width=20,textvariable=shengao).place(x=50,y=21)
Label(root,text='体重(kg)',width=6).place(x=1,y=55)
Entry(root,width=20,textvariable=tizhong).place(x=50,y=55)
Label(root,text='性别',width=6).place(x=1,y=89)
Radiobutton(root,text='男性',value=1).place(x=60,y=89)
Radiobutton(root,text='女性',value=0).place(x=128,y=89)
Label(root,text='正常人的BMI是18.5%-23.9%',width=25,foreground='red').place(x=10,y=120)
def lg():
sg=shengao.get()
tz=tizhong.get()
Button(root,text='计算',width=8,command=lg).place(x=25,y=150)
Button(root,text='取消',width=8,command=quit).place(x=110,y=150)
root.mainloop()
def login():
nm=name.get()
pw=password.get()
if nm=='202131216082' and pw=='2022':
then()
else:
messagebox.showwarning(title='警告',message='登陆失败,请检查账号密码')
Button(root,text='登录',width=8,command=login).place(x=25,y=140)
Button(root,text='取消',width=8,command=quit).place(x=110,y=140)
root.mainloop()

  • 写回答

3条回答 默认 最新

  • 天际的海浪 2022-06-02 19:34
    关注

    你字符串中是什么内容?
    字符串是是不是有数字之外字符,所以不能转为浮点数

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 6月2日
  • 已采纳回答 6月2日
  • 修改了问题 6月2日
  • 赞助了问题酬金5元 6月2日
  • 展开全部

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。