我写了下面的美丽python示例代码。现在我该如何做到这一点,所以当我退出然后重新启动程序时,它会记住秤的最后一个位置?如何让Python记住设置?
import Tkinter
root = Tkinter.Tk()
root.sclX = Tkinter.Scale(root, from_=0, to=1500, orient='horizontal', resolution=1)
root.sclX.pack(ipadx=75)
root.resizable(False,False)
root.title('Scale')
root.mainloop()
编辑:
我尝试下面的代码
import Tkinter
import cPickle
root = Tkinter.Tk()
root.sclX = Tkinter.Scale(root, from_=0, to=1500, orient='horizontal', resolution=1)
root.sclX.pack(ipadx=75)
root.resizable(False,False)
root.title('Scale')
with open('myconfig.pk', 'wb') as f:
cPickle.dump(f, root.config(), -1)
cPickle.dump(f, root.sclX.config(), -1)
root.mainloop()
但出现以下错误
Traceback (most recent call last):
File "<string>", line 244, in run_nodebug
File "C:\Python26\pickleexample.py", line 17, in <module>
cPickle.dump(f, root.config(), -1)
TypeError: argument must have 'write' attribute
我想这样,但出现以下错误: 回溯(最近通话最后一个): 文件“”,线路244,在run_nodebug 文件“C:\ Python26 \ pickleexample.py”,行30,在 与开放(CONFIG_FILE,'w')作为f: IOError:[Errno 2]没有这样的文件或目录:'库\\ Documents \\ T.txt' 如何正确设置文件目录? –
rectangletangle
2010-06-27 23:50:22
这可能意味着您尝试创建的文件的父目录之一不存在。确保你在'CONFIG_FILE'中指定的文件的父目录存在。例如,如果你设置了'CONFIG_FILE ='Libraries \ Documents \ T.txt'',确保目录'Libraries \ Documents'(相对于你运行Python脚本的目录)存在。尽管我实际上推荐使用绝对路径(一个以驱动器号开头的路径),例如'CONFIG_FILE ='C:\ SomeFolder \ Libraries \ Documents \ T.txt''。这种方式不那么令人困惑。 – 2010-06-28 00:14:43