OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How would I return an assigned variable of a class (monitor = Monitor()) as a tuple for a match case comparison?

  • Thread starter Thread starter phpjunkie
  • Start date Start date
P

phpjunkie

Guest
I have a Dell 2-in-1 computer that I can use as a tablet, and I'm working on a script to resize the window of an application base on the orientation of the display. What I'm trying to do is get an assigned variable monitor = Monitor() to return itself as a tuple in a match case comparison.

Code:
from win32api import GetMonitorInfo, MonitorFromPoint
from typing import Optional

class Monitor:
    def __init__(self):
        self._monitor: Optional[tuple] = None

        self.update()

    @property
    def Width(self) -> int:
        return self._monitor[0]

    @property
    def Height(self) -> int:
        return self._monitor[1]

    def update(self):
        self._monitor = self()

    def __call__(self) -> tuple:
        return GetMonitorInfo(MonitorFromPoint((0, 0)))['Monitor'][2:]

    def __iter__(self):
        return iter(self._monitor)

    def __next__(self):
        return tuple(self._monitor)

    def __getitem__(self, index):
        return self._monitor[index]

    def __eq__(self, other):
        return self._monitor == other

    def __str__(self):
        return str(self._monitor)

    def __repr__(self):
        return str(self._monitor)


if __name__ == '__main__':
    monitor = Monitor()
    print(f'monitor: {monitor}')

    match monitor:
        case (1440, 900):
            print((1440, 900))
        case (900, 1440):
            print((900, 1440))

For several days, I've read through posts here on stackoverflow.com, and I've been searching through Google search looking for code examples, but I haven't found anything. I do know that the class.__call__ will give me what I want, but in this class, the __call__ is to get the current resolution of the monitor so I can compare the class against itself monitor == monitor(). With Windows 11 Tablet Mode, the touch screen will rotate with the computer the same way a touch screen does on an Android device.
<p>I have a Dell 2-in-1 computer that I can use as a tablet, and I'm working on a script to resize the window of an application base on the orientation of the display. What I'm trying to do is get an assigned variable <code>monitor = Monitor()</code> to return itself as a tuple in a match case comparison.</p>
<pre class="lang-py prettyprint-override"><code>from win32api import GetMonitorInfo, MonitorFromPoint
from typing import Optional

class Monitor:
def __init__(self):
self._monitor: Optional[tuple] = None

self.update()

@property
def Width(self) -> int:
return self._monitor[0]

@property
def Height(self) -> int:
return self._monitor[1]

def update(self):
self._monitor = self()

def __call__(self) -> tuple:
return GetMonitorInfo(MonitorFromPoint((0, 0)))['Monitor'][2:]

def __iter__(self):
return iter(self._monitor)

def __next__(self):
return tuple(self._monitor)

def __getitem__(self, index):
return self._monitor[index]

def __eq__(self, other):
return self._monitor == other

def __str__(self):
return str(self._monitor)

def __repr__(self):
return str(self._monitor)


if __name__ == '__main__':
monitor = Monitor()
print(f'monitor: {monitor}')

match monitor:
case (1440, 900):
print((1440, 900))
case (900, 1440):
print((900, 1440))
</code></pre>
<p>For several days, I've read through posts here on stackoverflow.com, and I've been searching through Google search looking for code examples, but I haven't found anything. I do know that the <code>class.__call__</code> will give me what I want, but in this class, the <code>__call__</code> is to get the current resolution of the monitor so I can compare the class against itself <code>monitor == monitor()</code>. With Windows 11 Tablet Mode, the touch screen will rotate with the computer the same way a touch screen does on an Android device.</p>
 

Latest posts

Top