tkinter消息框中的格式化方法

时间:2017-02-23 16:52:39

标签: python python-3.x tkinter messagebox

我想使用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)

你知道我怎么能克服这个?

谢谢

2 个答案:

答案 0 :(得分:2)

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))

答案 1 :(得分:2)

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))