tkinter messagebox 中的格式化方法

Format method in tkinter messagebox

我想 return 使用 Python 3 中的 .format() 方法计算某个数值,并在其中使用 div 运算符(/ ).

但是消息框库不支持此功能。

        #Remind dilutions
    if self.initial_concentration > (1000):
        messagebox.INFO('Since your dilution is in the lowest range, consider a 1:100 pre-dilution first, so you would have {:2f}').format(answer)

你知道我该如何克服这个问题吗?

谢谢

messagebox.INFO('Since ... have {:2f}').format(answer)
#                                     ^
# calling `format` method of the return value of the `INFO(..)`,
#   (not against the formatting string)
# which may not exists; possibly causing AttributeError

以上行应替换为:

messagebox.INFO('Since ... have {:2f}'.format(answer))

format 是一个 str 函数,您应该从 str 而不是 INFO 使用它。

解决方案:

 messagebox.INFO('Since your dilution is in the lowest range, consider a 1:100 pre-dilution first, so you would have {:2f}'.format(answer))