pystray script - Resolution resets after tabbing out

21 Views Asked by At

If find this a rather "weird" issue, but maybe it can be explained (I probably don't have enough knowledge). The switcher normally works just fine, so let me explain further: I play some competitive games at a different resolution than my monitor's native, so I used cru to display scale at 1080p and 1050p. Normally I would switch using the display adapter window in advanced graphic settings, which kinda takes a lot of steps to switch every time. The thing is that if i set my monitor at 1080p through the adapter window, switch to 1050p using the tool, run the game on fullscreen and tab out, the resolution will reset to 1080p, which will - 1: make the screen black since it's switching; and 2: i will have to switch back to the resolution everytime i tab out, which is annoying.

Sorry for the whole paragraph.. now here's the code :)

# system_tray.py
def set_resolution(width: int, height: int):
    """This is what actually sets the resolution"""
    devmode = pywintypes.DEVMODEType()
    devmode.PelsWidth = width
    devmode.PelsHeight = height

    devmode.Fields = win32con.DM_PELSWIDTH | win32con.DM_PELSHEIGHT

    win32api.ChangeDisplaySettings(devmode, 0)


class SysTray:
    """This represents the system tray"""
    def __init__(self):
        self.image = Image.open('Utils/Silverscreen.png')
        self.menu = (
            item('1050p@165', lambda: set_resolution(1680, 1050)),
            item('1080p@165', lambda: set_resolution(1920, 1080)),
            item('Quit', self.on_quit)
        )
        self.icon = pystray.Icon('name', self.image, 'title', self.menu)

    def on_quit(self):
        """Hides and stops the tray when clicking quit"""
        self.icon.visible = False
        self.icon.stop()

    def run_tray(self):
        """Runs the tray"""
        self.icon.run()
# ResolutionSwitcher.py
from Utils.system_tray import SysTray  # Basically all the code is in here

if __name__ == '__main__':
    tray = SysTray()
    tray.run_tray()

I tried running the script from a command prompt with admin priviledges, because i was thinking this might be a permission issue, but that also didn't fix the thing. I also tried running the games in windowed fullscreen, and while that does "fix" the issue, it's not very optimal for performance.

0

There are 0 best solutions below