select certain monitor for going fullscreen with gtk

2024/11/18 20:28:08

I intend to change the monitor where I show a fullscreen window. This is especially interesting when having a projector hooked up.

I've tried to use fullscreen_on_monitor but that doesn't produce any visible changes.

Here is a non-working example:

#!/usr/bin/env python
import sysimport gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gdkw = Gtk.Window()screen = Gdk.Screen.get_default()
print ("Montors: %d" % screen.get_n_monitors())
if len(sys.argv) > 1:n = int(sys.argv[1])
else:n = 0l = Gtk.Button(label="Hello, %d monitors!" % screen.get_n_monitors())
w.add(l)
w.show_all()w.fullscreen_on_monitor(screen, n)
l.connect("clicked", Gtk.main_quit)
w.connect("destroy", Gtk.main_quit)
Gtk.main()

I get to see the window on the very same monitor (out of 3), regardless of the value I provide.

My question is: how do I make the fullscreen window appear on a different monitor?

Answer

The problem seems to be that Gtk just ignores the monitor number, it will always fullscreen the window on the monitor on which the window currently is positioned. This sucks, but we can use that to make it work the way we want to.

But first some theory about multiple monitors, they aren't actually separate monitors for your pc. It considers them to collectively form one screen which share the same global origin. On that global screen each monitor has a origin relative to the global origin, just like windows.

Because we know that Gtk will always fullscreen on the monitor on which the window is we can simply move the window to the origin of the monitor using window.move(x,y) and then call window.fullscreen().

(The move function will move the window to a position (x,y) relative to it's parent, which in the case of the main window is the global screen.)

Combining all this we get this, which works perfectly on Windows 10:

def fullscreen_at_monitor(window, n):screen = Gdk.Screen.get_default()monitor_n_geo = screen.get_monitor_geometry(n)x = monitor_n_geo.xy = monitor_n_geo.ywindow.move(x,y)window.fullscreen()
https://en.xdnf.cn/q/71020.html

Related Q&A

Load Excel add-in using win32com from Python

Ive seen from various questions on here that if an instance of Excel is opened from Python using:xl = win32com.client.gencache.EnsureDispatch(Excel.Application) xl.Visible = True wb = xl.Workbooks.Open…

iterating through a list removing items, some items are not removed

Im trying to transfer the contents of one list to another, but its not working and I dont know why not. My code looks like this:list1 = [1, 2, 3, 4, 5, 6] list2 = []for item in list1:list2.append(item)…

Apply function to create string with multiple columns as argument

I have a dataframe like this:name . size . type . av_size_type 0 John . 23 . Qapra . 22 1 Dan . 21 . nukneH . 12 2 Monica . 12 . kahless . 15I wa…

Popping items from a list using a loop in Python [duplicate]

This question already has answers here:Strange result when removing item from a list while iterating over it in Python(12 answers)Closed 3 months ago.Im trying to write a for loop in python to pop out …

Django Admin Media prefix URL issue

i ve the following folder structuresrc\BAT\templates\admin\base.html src\BAT\media\base.css src\BAT\media\admin-media\base.csssettings.pyMEDIA_ROOT = os.path.join( APP_DIR, media ) MEDIA_URL = /media/ …

lazy processpoolexecutor in Python?

I have a large number of tasks that I want to execute and make the results available via a generator. However, using a ProcessPoolExecutor and as_completed will evaluate the results greedily and store …

error occurs when installing cryptography for scrapy in virtualenv on OS X [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Can Python do DI seamlessly without relying on a service locator?

Im coming from the C# world, so my views may be a little skewed. Im looking to do DI in Python, however Im noticing a trend with libraries where they all appear to rely on a service locator. That is, y…

Producing pdf report from python with bullet points

I would like to produce a dynamic pdf document from a python script that looks like the image below. Each sentence starts with a bullet point, and the text and number of lines depends on what the user …

Converting bits to bytes in Python

I am trying to convert a bit string into a byte string, in Python 3.x. In each byte, bits are filled from high order to low order. The last byte is filled with zeros if necessary. The bit string is ini…