美文网首页PYQT5学习与开发
PYQT5(12)可复用的自动保存窗口位置的frame

PYQT5(12)可复用的自动保存窗口位置的frame

作者: 弗兰克万岁 | 来源:发表于2019-01-15 10:55 被阅读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'])

相关文章

网友评论

    本文标题:PYQT5(12)可复用的自动保存窗口位置的frame

    本文链接:https://www.haomeiwen.com/subject/rnvedqtx.html