这是我的代码:
from tkinter import *
def calculate():
temp = int(entry.get())
temp = 9/5*temp+32
output_label.configure(text = 'Converted: {:.lf}'.format(temp))
entry.delete(0,END)
root = Tk()
message_label = Label(text = 'Enter a temperature',
font=('Verdana', 16))
output_label = Label(font = ('Verdana', 16))
entry = Entry(font = ('Verdana', 16), width=4)
calc_button = Button(text ='ok', font=('Verdana', 16),
command=calculate)
message_label.grid(row=0, column=0)
entry.grid(row=0, column=1)
calc_button.grid(row=0, column=2)
output_label.grid(row=1, column=0, columnspan=3)
mainloop()
这是输出错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:UsersUSERAnaconda3envsnlp_courselibtkinter__init__.py", line 1705, in __call__
return self.func(*args)
File "<ipython-input-9-c6af4eb59ca6>", line 7, in calculate
output_label.configure(text = 'Converted: {:.lf}'.format(temp))
ValueError: Format specifier missing precision
有人能帮我解决这个问题吗?
您的格式字符串中有一个小错误:
'Converted: {:.lf}'
应该是
'Converted: {:.1f}'
唯一的区别是在指定浮点精度时使用了l
而不是1
。1f
意味着您的浮点值应该用一个小数位输出。
对我来说,就是这样
'Converted: {:.1.f}'
而不是正确的
'Converted: {:.1f}'
可能发生在格式说明符错误时