PYQT5(12)可复用的自动保存窗口位置的frame
2019-01-15 本文已影响58人
弗兰克万岁
import json
import os
from PyQt5.QtWidgets import QMainWindow
class AutoSaveWindow(QMainWindow):
def __init__(self,name=''):
#注意初始化的时候要带上name参数
super().__init__()
self.name=name
if os.path.exists('window_location_%s.txt'%self.name):
self.load_location()
#每次启动都检查位置并移动
def closeEvent(self, *args, **kwargs):
self.save_location()
def save_location(self):
with open('window_location_%s.txt'%self.name, 'w') as f:
data = {'x': self.x(), 'y': self.y()}
f.write(json.dumps(data))
def load_location(self):
with open('window_location_%s.txt'%self.name, 'r') as f:
txt = f.read()
print(txt)
j = json.loads(txt)
self.move(j['x'],j['y'])