如何让Python记住设置?

如何让Python记住设置?,python,memory,tkinter,python-2.x,Python,Memory,Tkinter,Python 2.x,我在下面编写了漂亮的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') roo

我在下面编写了漂亮的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

将比例值写入文件,并在启动时读取。这里有一个大致的方法

CONFIG_FILE = '/path/to/config/file'

root.sclX = ...

try:
    with open(CONFIG_FILE, 'r') as f:
        root.sclX.set(int(f.read()))
except IOError:    # this is what happens if the file doesn't exist
    pass

...
root.mainloop()

# this needs to run when your program exits
with open(CONFIG_FILE, 'w') as f:
    f.write(str(root.sclX.get()))

显然,如果您想保存和恢复其他值,可以使其更加健壮/复杂/复杂。

将比例值写入文件,并在启动时读取。这里有一个大致的方法

CONFIG_FILE = '/path/to/config/file'

root.sclX = ...

try:
    with open(CONFIG_FILE, 'r') as f:
        root.sclX.set(int(f.read()))
except IOError:    # this is what happens if the file doesn't exist
    pass

...
root.mainloop()

# this needs to run when your program exits
with open(CONFIG_FILE, 'w') as f:
    f.write(str(root.sclX.get()))

显然,如果您想保存和恢复其他值,可以使其更加健壮/复杂/复杂。

您可以告诉程序使用参数编写一个名为save.txt的文件,然后在进一步执行时加载它:

是否有save.txt文件

否:使用参数写入新的保存文件。 是:读取参数并将其传递给变量

如果参数已更新,则在文件中重写它


我不是Python方面的专家,但应该有一些很好的库来读写文件:

您可以告诉程序使用参数编写一个名为save.txt的文件,然后在进一步执行时加载它:

是否有save.txt文件

否:使用参数写入新的保存文件。 是:读取参数并将其传递给变量

如果参数已更新,则在文件中重写它

我不是Python专家,但是应该有一些很好的库来读写文件:

就在主循环之前:

而且,在.pk文件已经存在的后续运行中,相应的cPickle.load调用将其取回,并使用…config**k对其进行设置,不幸的是,还需要一些技巧来向cPickle确认pickle配置可以安全地重新加载。

就在主循环之前:

而且,在.pk文件已经存在的后续运行中,相应的cPickle.load调用将其取回,并使用…config**k将其设置,不幸的是,还需要一些技巧来向cPickle确认pickle配置可以安全地重新加载。

help(cPickle.dump)
在中,解释器将立即显示您应该尝试通过交换f和调用中存储的对象来更改Alex的代码:

cPickle.dump(root.config(), f, -1)
cPickle.dump(root.sclX.config(), f, -1)
简单使用

help(cPickle.dump)
在中,解释器将立即显示您应该尝试通过交换f和调用中存储的对象来更改Alex的代码:

cPickle.dump(root.config(), f, -1)
cPickle.dump(root.sclX.config(), f, -1)

对于objet持久性,Python中有一个很好的模块:shelve

e、 g.两个脚本位于同一目录中

脚本1

import shelve

settings = shelve.open('mySettings')
settings['vroot_directory'] = commands.getoutput("pwd")
脚本2

import shelve 

settings = shelve.open('mySettings')
root_directory_script1 = settings['vroot_directory'])   
rgds

注:关于更完整的例子,请参见g.d.d.c.在我的帖子中的答案:

对于objet持久化,Python中有一个很好的模块:shelve

e、 g.两个脚本位于同一目录中

脚本1

import shelve

settings = shelve.open('mySettings')
settings['vroot_directory'] = commands.getoutput("pwd")
脚本2

import shelve 

settings = shelve.open('mySettings')
root_directory_script1 = settings['vroot_directory'])   
rgds

注:关于更完整的例子,请参见g.d.d.c.在我的帖子中的答案:

我尝试了这种方法,但得到了以下错误:回溯最近的调用last:File,第244行,在run\u nodebug文件C:\Python26\pickle example.py,第30行,在with openCONFIG\u文件中,“w”作为f:IOError:[Errno 2]没有这样的文件或目录:“Libraries\\Documents\\T.txt”如何正确设置文件目录?这可能意味着您试图创建的文件的父目录之一不存在。确保在CONFIG_文件中指定的文件的父目录存在。例如,如果设置CONFIG_FILE='Libraries\Documents\T.txt',请确保与运行Python脚本的目录相关的目录Libraries\Documents存在。尽管我实际上建议使用以驱动器号开头的绝对路径,例如CONFIG_FILE='C:\SomeFolder\Libraries\Documents\T.txt'。这样就不那么容易混淆了。我尝试了这种方法,但得到了以下错误:回溯最近的调用last:File,第244行,在run\u nodebug文件C:\Python26\pickle example.py,第30行,在带有openCONFIG\u文件的文件中,'w'as f:IOError:[Errno 2]没有这样的文件或目录:“Libraries\\Documents\\T.txt”如何正确设置文件目录?这可能意味着您试图创建的文件的父目录之一不存在。确保在CONFIG_文件中指定的文件的父目录存在。例如,如果设置CONFIG_FILE='Libraries\Documents\T.txt',请确保与运行Python脚本的目录相关的目录Libraries\Documents存在。尽管我实际上建议使用以驱动器号开头的绝对路径,例如CONFIG_FILE='C:\SomeFolder\Libraries\Documents\T.txt'。这样就不那么令人困惑了。