How do I get monitor resolution in Python?

后端 未结 30 1538
深忆病人
深忆病人 2020-11-22 13:49

What is the simplest way to get monitor resolution (preferably in a tuple)?

30条回答
  •  半阙折子戏
    2020-11-22 14:36

    Expanding on @user2366975's answer, to get the current screen size in a multi-screen setup using Tkinter (code in Python 2/3):

    try:
        # for Python 3
        import tkinter as tk
    except ImportError:
        # for Python 2
        import Tkinter as tk
    
    
    def get_curr_screen_geometry():
        """
        Workaround to get the size of the current screen in a multi-screen setup.
    
        Returns:
            geometry (str): The standard Tk geometry string.
                [width]x[height]+[left]+[top]
        """
        root = tk.Tk()
        root.update_idletasks()
        root.attributes('-fullscreen', True)
        root.state('iconic')
        geometry = root.winfo_geometry()
        root.destroy()
        return geometry
    

    (Should work cross-platform, tested on Linux only)

提交回复
热议问题