Adventure-Knorrig
Adventure-Knorrig

Reputation: 328

How to open Tkinter GUI on second monitor/display? (Windows)

I'd like to incorporate a way to have my GUI open on a second monitor, if available. I want to add in some type of error handling so if there is a second monitor, use it, else open on the center of the detected display. Can this be done?

Upvotes: 4

Views: 5895

Answers (2)

Reblochon Masque
Reblochon Masque

Reputation: 36682

On MacOS, use Appkit:

import AppKit

[(screen.frame().size.width, screen.frame().size.height)
    for screen in AppKit.NSScreen.screens()]

will give you a list of tuples containing all screen sizes (if multiple monitors present)

Upvotes: 1

RFairey
RFairey

Reputation: 776

I've been using this snippet using the win32 API to enumerate monitors on windows:

import ctypes
user = ctypes.windll.user32

class RECT(ctypes.Structure):
  _fields_ = [
    ('left', ctypes.c_long),
    ('top', ctypes.c_long),
    ('right', ctypes.c_long),
    ('bottom', ctypes.c_long)
    ]
  def dump(self):
    return [int(val) for val in (self.left, self.top, self.right, self.bottom)]

class MONITORINFO(ctypes.Structure):
  _fields_ = [
    ('cbSize', ctypes.c_ulong),
    ('rcMonitor', RECT),
    ('rcWork', RECT),
    ('dwFlags', ctypes.c_ulong)
    ]

def get_monitors():
  retval = []
  CBFUNC = ctypes.WINFUNCTYPE(ctypes.c_int, ctypes.c_ulong, ctypes.c_ulong, ctypes.POINTER(RECT), ctypes.c_double)
  def cb(hMonitor, hdcMonitor, lprcMonitor, dwData):
    r = lprcMonitor.contents
    #print("cb: %s %s %s %s %s %s %s %s" % (hMonitor, type(hMonitor), hdcMonitor, type(hdcMonitor), lprcMonitor, type(lprcMonitor), dwData, type(dwData)))
    data = [hMonitor]
    data.append(r.dump())
    retval.append(data)
    return 1
  cbfunc = CBFUNC(cb)
  temp = user.EnumDisplayMonitors(0, 0, cbfunc, 0)
  #print(temp)
  return retval

def monitor_areas():
  retval = []
  monitors = get_monitors()
  for hMonitor, extents in monitors:
    data = [hMonitor]
    mi = MONITORINFO()
    mi.cbSize = ctypes.sizeof(MONITORINFO)
    mi.rcMonitor = RECT()
    mi.rcWork = RECT()
    res = user.GetMonitorInfoA(hMonitor, ctypes.byref(mi))
    data = mi.rcMonitor.dump()
#    data.append(mi.rcWork.dump())
    retval.append(data)
  return retval


if __name__ == "__main__":
  print(monitor_areas())

On my system that prints [[0, 0, 3440, 1440], [3440, 0, 5120, 1050]]

In tkinter you can then move your app window to a given location with

appwindow.geometry('+1720+720')

Which puts it 1720px in from the left and 720 down from the top.

Any monitors that are to the left of or above your primary monitor may get negative coordinates, but they should work just fine.

Upvotes: 11

Related Questions