Я видел этот вопрос, когда пытался ответить на него: Как сохранить графический интерфейс в файл в GtkD?
Демонстрационный код изменен, чтобы ответить на ваш вопрос, и здесьэто:
#!/usr/bin/env python3
from gi.repository import Gtk
import json
import os
class Demo():
def __init__(self):
self.conf_name = 'demo.json'
self.read_config()
self.init_widgets()
self.init_window_position()
def init_widgets(self):
self.window = Gtk.Window()
self.window.set_title('window 1')
self.window.connect('check-resize', self.on_window_check_resize)
self.window.connect('configure-event',
self.on_window_configure_event)
self.window.connect('destroy', self.on_app_exit)
label = Gtk.Label('hello, world')
self.window.add(label)
self.window2 = Gtk.Window()
self.window2.set_title('window 2')
self.window2.connect('configure-event',
self.on_window2_configure_event)
def init_window_position(self):
# restore window size and position.
# we need to read these values from app configuration.
self.window.move(self.conf['window_x'], self.conf['window_y'])
self.window2.move(self.conf['window_x'] + self.conf['pos_diff_x'],
self.conf['window_y'] + self.conf['pos_diff_y'])
self.window.set_default_size(self.conf['window_width'],
self.conf['window_height'])
def read_config(self):
if os.path.isfile(self.conf_name):
with open(self.conf_name) as fh:
self.conf = json.loads(fh.read())
else:
# default configuration.
self.conf = {
'window_x': 100,
'window_y': 100,
'window_width': 320,
'window_height': 240,
# distance between window1 and window2 in x dimention.
'pos_diff_x': 120,
# distance between window1 and window2 in y dimention.
'pos_diff_y': 120,
}
def write_config(self):
with open(self.conf_name, 'w') as fh:
fh.write(json.dumps(self.conf))
def run(self):
self.window.show_all()
self.window2.show_all()
Gtk.main()
def on_app_exit(self, widget):
# new setting is dumped when app exits.
self.write_config()
Gtk.main_quit()
def on_window_check_resize(self, window):
print('check resize')
width, height = self.window.get_size()
print('size: ', width, height)
self.conf['window_width'] = width
self.conf['window_height'] = height
def on_window_configure_event(self, window, event):
print('configure event')
x, y = self.window.get_position()
print('position:', x, y)
self.conf['window_x'] = x
self.conf['window_y'] = y
# now move window2.
self.window2.move(x + self.conf['pos_diff_x'],
y + self.conf['pos_diff_y'])
def on_window2_configure_event(self, window, event):
'''
If window2 is moved by user, pos_diff_x and pos_diff_y need to be
recalculated.
'''
print('window2 configure event')
x2, y2 = self.window2.get_position()
print('position: ', x2, y2)
self.conf['pos_diff_x'] = x2 - self.conf['window_x']
self.conf['pos_diff_y'] = y2 - self.conf['window_y']
if __name__ == '__main__':
demo = Demo()
demo.run()
И скриншот: