아래에 아름다운 파이썬 예제 코드를 작성했습니다. 이제 어떻게 끝내고 프로그램을 다시 시작하면 저울의 마지막 위치를 기억할 수 있을까요?어떻게 파이썬이 설정을 기억하게합니까?
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"파일을 열고 (CONFIG_FILE, 'w') f : IOError : [Errno 2] 해당 파일이나 디렉토리가 없습니다. '라이브러리 \\ Documents \\ T.txt ' 어떻게 파일 디렉토리를 올바르게 설정합니까? –
rectangletangle
아마도 생성하려는 파일의 상위 디렉토리 중 하나가 존재하지 않는다는 것을 의미합니다. 'CONFIG_FILE'에서 지정한 파일의 부모 디렉토리가 존재하는지 확인하십시오. 예를 들어,'CONFIG_FILE = 'Libraries \ Documents \ T.txt'를 설정했다면, Python 스크립트를 실행하는 디렉토리에 상대적인'Libraries \ Documents' 디렉토리가 존재하는지 확인하십시오. 절대 경로 (드라이브 문자로 시작하는 경로)를 사용하는 것이 좋습니다. 'CONFIG_FILE = 'C : \ SomeFolder \ Libraries \ Documents \ T.txt'로 설정하십시오. 그렇게 혼란스럽지 않습니다. –