首页 > 解决方案 > 无法将字符串转换为浮点数(Tkinter)

问题描述

我正在尝试构建一个使用浮点变量的 Minecraft 要塞坐标查找器,这是我的代码:

from tkinter import *
from math import tan
from math import radians
def clicked():
    global x1, z1, f1, x2, z2, f2, x, z
    x1=float(x1_string)
    z1=float(z1_string)
    f1=float(f1_string)
    x2=float(x2_string)
    z2=float(z2_string)
    f2=float(f2_string)
    if f1 == 90.0 or f1 == -90.0:
        z = float(z1)
        x = float(tan(radians(-1*f2))*z + x2 - tan(radians(-1*f2)*z2))
    elif f2 == 90.0 or f2 == -90.0:
        z = float(z2)
        x = float(tan(radians(-1*f1))*z + x1 - tan(radians(-1*f1))*z1)
    else:
        z = float((x2 - tan(radians(-1*f2))*z2 - x1 + tan(radians(-1*f1))*z1)/(tan(radians(-1*f1)) - tan(radians(-1*f2))))
        x = float(tan(radians(-1*f1))*z + x1 - tan(radians(-1*f1))*z1)
    x = round(x)
    z = round(z)
    
    
window = Tk()
window.title("Minecraft Stronghold Finder")
window.geometry('1280x720')
text = Label(window, text="Welcome to the Minecraft Stronghold Finder", font = ('Arial Bold', 20))
text.grid(column = 0, row = 0)
text1 = Label(window, text="Input your coordinates and angles of view", font = ('Arial', 15))
text1.place(x = 0, y = 30)
text2 = Label(window, text="First throw:", font = ('Arial', 15))
text2.place(x = 0, y = 55)
text3 = Label(window, text="Second throw:", font = ('Arial', 15))
text3.place(x = 0, y = 105)
tx1 = Label(window, text="X:", font = ('Arial Bold', 15))
tx1.place(x = 0, y = 80)
ex1 = Entry(window, width = 10)
ex1.place(x = 25, y = 85)
tz1 = Label(window, text="Z:", font = ('Arial Bold', 15))
tz1.place(x = 100, y = 80)
ez1 = Entry(window, width = 10)
ez1.place(x = 125, y = 85)
tang1 = Label(window, text="Angle:", font = ('Arial Bold', 15))
tang1.place(x = 200, y = 80)
eang1 = Entry(window, width = 10)
eang1.place(x = 260, y = 85)
tx2 = Label(window, text="X:", font = ('Arial Bold', 15))
tx2.place(x = 0, y = 130)
ex2 = Entry(window, width = 10)
ex2.place(x = 25, y = 135)
tz2 = Label(window, text="Z:", font = ('Arial Bold', 15))
tz2.place(x = 100, y = 130)
ez2 = Entry(window, width = 10)
ez2.place(x = 125, y = 135)
tang2 = Label(window, text="Angle:", font = ('Arial Bold', 15))
tang2.place(x = 200, y = 130)
eang2 = Entry(window, width = 10)
eang2.place(x = 260, y = 135)
btn = Button(window, text="Calculate!", font = ('Arial Bold', 15), command = clicked)
btn.place(x = 350, y = 95)
x1_string=format(ex1.get)
z1_string=format(ez1.get)
f1_string=format(eang1.get)
x2_string=format(ex2.get)
z2_string=format(ez2.get)
f2_string=format(eang2.get)
    
textx = Label(window, text=x, font=('Arial Bold', 15))
textz = Label(window, text=z, font=('Arial Bold', 15))
textx.place(x = 155, y = 240)
textz.place(x = 165, y = 240)
text4 = Label(window, text='X:', font=('Arial Bold', 15))
text4.place(x = 140, y = 90)
text5 = Label(window, text='Z:', font=('Arial Bold', 15))
text5.place(x = 150, y = 90)
window.mainloop()

Python 不断给我以下错误,其中一个是不可能将 string 转换为 float,另一个是python 看不到我的 x 和 z 变量,但我将它们声明为全局的:

Traceback (most recent call last):
Minecraft Strongholf Finder.py", line 77, in <module>
    textx = Label(window, text=x, font=('Arial Bold', 15))
NameError: name 'x' is not defined
>>> Exception in Tkinter callback
Traceback (most recent call last):
Minecraft Strongholf Finder.py", line 9, in clicked
    x1=float(x1_string)
ValueError: could not convert string to float: '<bound method Entry.get of <tkinter.Entry object .!entry>>'

我是编程新手,所以请不要太苛刻地判断。谢谢你们 :)

标签: pythonuser-interfacetkinter

解决方案


当您.get()在小部件初始化后立即调用该方法时,它们将永久保存一个空字符串,并且在转换为float它时会引发错误。

我对您的代码进行了一些更改,请参阅下面的代码

from tkinter import *
from math import tan
from math import radians
def clicked():
    global ex1, ez1, eang1, ex2, ez2, eang2, textx, textz
    x1=float(format(ex1.get()))
    z1=float(format(ez1.get()))
    f1=float(format(eang1.get()))
    x2=float(format(ex2.get()))
    z2=float(format(ez2.get()))
    f2=float(format(eang2.get()))
    if f1 == 90.0 or f1 == -90.0:
        z = float(z1)
        x = float(tan(radians(-1*f2))*z + x2 - tan(radians(-1*f2)*z2))
    elif f2 == 90.0 or f2 == -90.0:
        z = float(z2)
        x = float(tan(radians(-1*f1))*z + x1 - tan(radians(-1*f1))*z1)
    else:
        z = float((x2 - tan(radians(-1*f2))*z2 - x1 + tan(radians(-1*f1))*z1)/(tan(radians(-1*f1)) - tan(radians(-1*f2))))
        x = float(tan(radians(-1*f1))*z + x1 - tan(radians(-1*f1))*z1)
    x = round(x)
    z = round(z)
    textx.config(text=str(x))
    textz.config(text=str(z))
    
    
window = Tk()
window.title("Minecraft Stronghold Finder")
window.geometry('1280x720')
text = Label(window, text="Welcome to the Minecraft Stronghold Finder", font = ('Arial Bold', 20))
text.grid(column = 0, row = 0)
text1 = Label(window, text="Input your coordinates and angles of view", font = ('Arial', 15))
text1.place(x = 0, y = 30)
text2 = Label(window, text="First throw:", font = ('Arial', 15))
text2.place(x = 0, y = 55)
text3 = Label(window, text="Second throw:", font = ('Arial', 15))
text3.place(x = 0, y = 105)
tx1 = Label(window, text="X:", font = ('Arial Bold', 15))
tx1.place(x = 0, y = 80)
ex1 = Entry(window, width = 10)
ex1.place(x = 25, y = 85)
tz1 = Label(window, text="Z:", font = ('Arial Bold', 15))
tz1.place(x = 100, y = 80)
ez1 = Entry(window, width = 10)
ez1.place(x = 125, y = 85)
tang1 = Label(window, text="Angle:", font = ('Arial Bold', 15))
tang1.place(x = 200, y = 80)
eang1 = Entry(window, width = 10)
eang1.place(x = 260, y = 85)
tx2 = Label(window, text="X:", font = ('Arial Bold', 15))
tx2.place(x = 0, y = 130)
ex2 = Entry(window, width = 10)
ex2.place(x = 25, y = 135)
tz2 = Label(window, text="Z:", font = ('Arial Bold', 15))
tz2.place(x = 100, y = 130)
ez2 = Entry(window, width = 10)
ez2.place(x = 125, y = 135)
tang2 = Label(window, text="Angle:", font = ('Arial Bold', 15))
tang2.place(x = 200, y = 130)
eang2 = Entry(window, width = 10)
eang2.place(x = 260, y = 135)
btn = Button(window, text="Calculate!", font = ('Arial Bold', 15), command = clicked)
btn.place(x = 350, y = 95)
    
textx = Label(window, font=('Arial Bold', 15))
textz = Label(window, font=('Arial Bold', 15))
textx.place(x = 155, y = 240)
textz.place(x = 175, y = 240)
text4 = Label(window, text='X:', font=('Arial Bold', 15))
text4.place(x = 140, y = 90)
text5 = Label(window, text='Z:', font=('Arial Bold', 15))
text5.place(x = 150, y = 90)
window.mainloop()

笔记

  • 我没有将检索到的值设为全局,而是将Text实例设为全局,然后.get()在函数中应用,clicked()然后加上大小写,以便选取条目中的当前值。
  • 不需要带有变量的标签,x最初z因为更改 x 和 z 的值不会影响标签的值,您可以使用.config()直接从函数中更改值。
  • 创建一个条件以确保您只使用角度的非零值,否则您最终将除以 0 并且将引发此错误ZeroDivisionError: float division by zero

推荐阅读