Multiple monitor

Hello Community,

I am trying to find the interface names of physical monitors connected to my ubuntu machine. I am able to achieve it using gtk libraries like get_n_monitors() and get_monitor_plug_name() but unfortunately these APIs are deprecated. So i am trying to use the alternate gdk api given in the api reference guide. But i am unable to find where the mistake is. Can someone please help me with this? I am basically trying to show a widget in individual monitors connected at some co-ordinates. I am also trying to find the interface names of individual monitors connected like this

{0: 'DP-3', 1: 'DP-1', 2: 'DP-2', 3: 'DisplayPort-1-5'}

Code using deprecated apis:

import gi
import sys
gi.require_version('Gdk', '3.0')
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk as gtk, GObject, Gdk, GLib
from gi import require_version
require_version("Gdk", "3.0")
from gi.repository import Gdk, Gtk as gtk

class PyApp(gtk.Window):

    def __init__(self, param_lable):
        super(PyApp, self).__init__()
        interval = 6000
        lable = gtk.Label(param_lable)
        self.set_size_request(100, 100)
        self.connect("destroy", gtk.main_quit)
        GLib.timeout_add(interval, gtk.main_quit)

def show_at_monitor(window, n):
    display = Gdk.Display.get_default()
    monitor = Gdk.Display.get_monitor(display, n)
    geometry = monitor.get_geometry()
    x = geometry.x
    y = geometry.y
def main():
    window = gtk.Window()
    screen = window.get_screen()
    nmons = screen.get_n_monitors()
    list_of_connected_monitors = []
    dict_plug_name_mon = {}
    for monitors in range(0, nmons):
        dict_plug_name_mon[monitors] = (str(screen.get_monitor_plug_name(monitors)))

for monitors in range(0, nmons):
    papp_obj = PyApp(str(monitors + 1))
    show_at_monitor(papp_obj, monitors)

the code above completely solves my purpose. but it also gives additional 
error saying some of the api is deprecated and it is asking me to use latest 
api which i dont know how to use.
# display = Gdk.Display().get_default()
# connected_monitors = display.get_n_monitors()
# monitor_interface_name = {}
# monitor = Gdk.Display.get_name(Gdk.Display.get_default())
# print(display.get_monitor_plug_name(monitor))

# for each_monitor in range (0, connected_monitors):
#     monitor = display.get_monitor(each_monitor)

    # monitor_interface_name[each_monitor] = str(monitor.get_model())
# print(monitor_interface_name)
# for monitors in range (0, number_of_monitor):
#     nth_display = Gdk.get_display()
#     dict_plug_name_mon[monitors] = (str(Gdk.Monitor.get_model(nth_display)))
# # gtk.main()
# print(dict_plug_name_mon)

if __name__ == '__main__':

Error if i use the deprecated api:

~/pygtk_experiment$ python3 DeprecationWarning: Gdk.Screen.get_n_monitors is deprecated
nmons = screen.get_n_monitors() DeprecationWarning: Gdk.Screen.get_monitor_plug_name is deprecated
dict_plug_name_mon[monitors] = (str(screen.get_monitor_plug_name(monitors))) PyGTKDeprecationWarning: Using positional arguments
with the GObject constructor has been deprecated. 
Please specify keyword(s) for "label" 
or use a class specific constructor. 
See:  /InitializerDeprecations
lable = gtk.Label(param_lable)
{0: 'DP-3', 1: 'DP-1', 2: 'DP-2', 3: 'DisplayPort-1-5'}

Thanks in advance.


Figured out with some trial and error. The below code would do that with new set of APIs.

monitor_interface_name = {}
display = Gdk.Display.get_default()
connected_monitor_count = Gdk.Display.get_n_monitors(display)
for monitors in range(0, connected_monitor_count):
    monitor = Gdk.Display.get_monitor(display, monitors)
    monitor_interface_name[monitors] = Gdk.Monitor.get_model(monitor)

Leaving this answer here without deleting thinking that this could help some one.


1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.