moving a window widget
I have two windows, one main window which lies behind second window.
How can i move the second window along with main window, when ever the main 开发者_JS百科window
moves. And second window is ordinary window, it will have reference to main window.
Regards, iSight
I saw this question when I tried to answer this one: How do you save a gui to file in GtkD?
The demo code is modified to answer your question, and here it is:
#!/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()
And a screenshot:
you can connect to the configure-event of your first window and change position of the second window accordingly to x and y coordinates which are coming via GdkEvent structure. Below is a small example:
void configure_callback(GtkWindow *window, GdkEvent *event, gpointer data)
{
int x = event->configure.x;
int y = event->configure.y;
gtk_widget_set_uposition(GTK_WIDGET(data), x+220, y);
}
int main(int argc, char * argv[])
{
gtk_init(&argc, &argv);
GtkWidget *window0 = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window0), "Window 0");
gtk_widget_set_uposition(window0, 20, 40);
GtkWidget *window1 = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window1), "Window 1");
gtk_widget_set_uposition(window1, 240, 40);
g_signal_connect_swapped(G_OBJECT(window0), "destroy",
G_CALLBACK(gtk_main_quit), G_OBJECT(window0));
g_signal_connect(G_OBJECT(window0), "configure-event",
G_CALLBACK(configure_callback), window1);
gtk_widget_show_all(window0);
gtk_widget_show_all(window1);
gtk_main();
return 0;
}
once you move "window 0", "window 1" should change its position accordingly.
hope this helps, regards
精彩评论