Everytime I connect to a specific external monitor, the default resolution is not the one I want, and I have to set it again.
How do I save a specific resolution for a specific display, and switch off my default display if I connect that display?
81 Answer
Set resolution, depending on the presence of a (specific) attached screen
Below two options:
- Set screen resolution and switch screen (automatic detection of second screen) by a shortcut key
- Run a background script to automatically switch off the main screen and change the resolution
Option 1; shortcut
#!/usr/bin/env python3
import subprocess
import time
# set the default screen
default = "DVI-I-1"
# set the specific external screen
external = "VGA-1"
# set the resolution of the single screen setup
singleres = "1680x1050"
# set the resolution of the specific external screeen
extrares = "1280x1024"
def get(cmd): return subprocess.check_output(cmd).decode("utf-8")
def run(cmd): subprocess.call(cmd)
def get_screens(): return [l.split()[0] for l in get("xrandr").splitlines() if " connected" in l]
def set_screen(n_scr, screens): if n_scr == 1: run(["xrandr", "--output", default, "--auto"]) run(["xrandr", "-s", singleres]) print("1 screen") elif all([n_scr == 2, external in screens]): run(["xrandr", "--output", default, "--off"]) run(["xrandr", "-s", extrares]) print("2 screens")
screens = get_screens()
n_scr2 = len(screens)
set_screen(n_scr2, screens)Option 2; a background version
#!/usr/bin/env python3
import subprocess
import time
# set the default screen
default = "DVI-I-1"
# set the specific external screen
external = "VGA-1"
# set the resolution of the single screen setup
singleres = "1680x1050"
# set the resolution of the specific external screeen
extrares = "1280x1024"
def get(cmd): try: return subprocess.check_output(cmd).decode("utf-8") except subprocess.CalledProcessError: pass
def run(cmd): subprocess.call(cmd)
def get_screens(scrdata): return [l.split()[0] for l in scrdata.splitlines() if " connected" in l]
def set_screen(n_scr, screens): if n_scr == 1: run(["xrandr", "--output", default, "--auto"]) run(["xrandr", "-s", singleres]) print("1 screen") elif all([n_scr == 2, external in screens]): run(["xrandr", "--output", default, "--off"]) run(["xrandr", "-s", extrares]) print("2 screens")
n_scr1 = None
while True: time.sleep(4) scrdata = get("xrandr") if scrdata: screens = get_screens(scrdata) n_scr2 = len(screens) if n_scr2 != n_scr1: set_screen(n_scr2, screens) n_scr1 = n_scr2How to use
- Copy either one of the scripts above into an empty file, save it as
set_screens.py Replace in the head section of the script the values for:
# set the default screen default = "DVI-I-1" # set the specific external screen external = "VGA-1" # set the resolution of the single screen setup singleres = "1680x1050" # set the resolution of the specific external screeen extrares = "1280x1024"(the current settings are just for my test setup)
Test- run and apply the script:
if you use option 1, the shortcut:
Open a terminal, run the script subsequently with and without the external screen, with the command:
python3 /path/to/set_screens.pyIt should set the screens as intended.
Subsequently add, if all works fine, the script to a shortcut: Choose: System Settings > "Keyboard" > "Shortcuts" > "Custom Shortcuts". Click the "+" and add the command:
python3 /path/to/set_screens.pyif you use option 2, the background script:
Open a terminal, run the script with the command:
python3 /path/to/set_screens.pyand connect / disconnect the external monitor. It should change the resolution and switch on / off your default monitor as intended.
Subsequently add, if all works fine, the script to Startup Applications: Dash > Startup Applications > Add. Add the command:
/bin/bash -c "sleep 10 && python3 /path/to/set_screens.py"