python pygtk how to put a window on a specific display monitor

╄→尐↘猪︶ㄣ 提交于 2019-12-10 10:00:45

问题


I'm using Python PyGTK library on OS Linux based (suse, ubuntu) Working on devices with multiple Display Monitors.

I'm trying to put a Full Screen Window (in python, gtk.Window) on a Specific Display monitor connected to the Device.

I'm using the following code:

n_monitors = gtk.gdk.Screen.get_n_monitors()  # Get number of monitors. 
gtk.gdk.Screen.get_monitor_geometry(*monitor_number*)  # Get the size of a specific monitor. 

The second api returns the monitor size and offset information. gtk.Window.move(offset_x, offse_y) can be used for moving a window on a specific monitor.

Though this doesn't seem to work all the time. It looks like the window has an affinity to the mouse location or if the monitor resolutions are not the same this does not work.

Is there any property of the gtk.Window which would help mitigate this issue. I tried playing with following which didn't help :

gtk.Window.set_position(gtk.WIN_POS_NONE)
gtk.Window.set_gravity(gtk.gdk.GRAVITY_NORTH_WEST)

Any ideas.


回答1:


Your monitors collectively form one screen. You can query a screen to find out about its monitors. You can then position your window on a given monitor by ofsetting the window's (x,y) with that of the monitor.

Assuming your window is window, you can get the screen like this

screen = window.get_screen()

and then get the monitor data like this

monitors = []
for m in range(screen.get_n_monitors())
  monitors.append(screen.get_monitor_geometry(m)

monitors are numbers 0,1,2,... You can get the current monitor, if you need to, like this:

screen.get_monitor_at_window(screen.get_active_window())

To position a window on a monitor, you add the monitor's (x,y) to the window's (x,y). Calculate those x,y values and then move the window:

window.move(x,y)

See a similar answer I provided here.



来源:https://stackoverflow.com/questions/22772968/python-pygtk-how-to-put-a-window-on-a-specific-display-monitor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!