我有两个窗口,一个主窗口位于第二个窗口后面。
当主窗口
时,如何将第二个窗口与主窗口一起移动移动。第二个窗口是普通窗口,它将引用主窗口。
此致 iSight的
答案 0 :(得分:0)
您可以连接到第一个窗口的configure-event,并相应地将第二个窗口的位置更改为通过GdkEvent结构的x和y坐标。以下是一个小例子:
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;
}
移动“窗口0”后,“窗口1”应相应地改变其位置。
希望这有帮助,尊重
答案 1 :(得分:0)
当我尝试回答这个问题时,我看到了这个问题:How do you save a gui to file in 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()
截图: