我想,我在输入字段中输入的内容应该自动四舍五入到n个小数点。
import Tkinter as Tk
root = Tk.Tk()
class InterfaceApp():
def __init__(self,parent):
self.parent = parent
root.title("P")
self.initialize()
def initialize(self):
frPic = Tk.Frame(bg='', colormap='new')
frPic.grid(row=0)
a= Tk.DoubleVar()
self.entry = Tk.Entry(frPic, textvariable=a)
a.set(round(self.entry.get(), 2))
self.entry.grid(row=0)
if __name__ == '__main__':
app = InterfaceApp(root)
root.mainloop()
我想你想要的不是四舍五入浮点值本身,而是要显示一个精度为n个小数点的浮点值。试试这个:
注意:在代码中,您尝试取整
self.entry
,也就是说,您尝试取整Tk.Entry
类型的实例。您应该使用self.entry.get()
,它为您提供一个字符串。在如果您不熟悉这种字符串格式,我使用look here。在
您没有得到预期的结果,因为当您在
initialize()
内运行a.set(round(self.entry, 2))
时,self.entry.get()
的值总是0
(创建后的默认值)您需要将一个callback附加到一个按钮小部件上,按下后,您要查找的行为将在该小部件上执行:
相关问题 更多 >
编程相关推荐