How can I get the screen size in Tkinter?
I would like to know if it is possible to calculate the 开发者_开发问答screen size using Tkinter.
I wanted this so that can make the program open up in the center of the screen...
import tkinter as tk
root = tk.Tk()
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
A possible solution
import os
os.system("xrandr | grep \* | cut -d' ' -f4")
My output:
1440x900
0
For Windows:
You can make the process aware of DPI to handle scaled displays.
import ctypes
try: # Windows 8.1 and later
ctypes.windll.shcore.SetProcessDpiAwareness(2)
except Exception as e:
pass
try: # Before Windows 8.1
ctypes.windll.user32.SetProcessDPIAware()
except: # Windows 8 or before
pass
Expanding on mouad's answer, this function is capable of handling multi-displays and returns the resolution of the current screen:
import tkinter
def get_display_size():
root = tkinter.Tk()
root.update_idletasks()
root.attributes('-fullscreen', True)
root.state('iconic')
height = root.winfo_screenheight()
width = root.winfo_screenwidth()
root.destroy()
return height, width
精彩评论