Reputation: 2094
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...
Upvotes: 47
Views: 85952
Reputation: 51
Getting the width and height value of the display is just a function, so it increases the transparency of the window, which can make users feel unknown.
import tkinter as tk
def get_display_size():
root = tk.Tk()
# set the Tk window to transparent
root.attributes("-alpha", 0)
display_height = root.winfo_screenheight()
display_width = root.winfo_screenwidth()
root.destroy()
return display_width, display_height
Upvotes: 0
Reputation: 5107
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
Upvotes: 8
Reputation: 70059
import tkinter as tk
root = tk.Tk()
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
Upvotes: 110
Reputation: 593
A possible solution
import os
os.system("xrandr | grep \* | cut -d' ' -f4")
My output:
1440x900
0
Upvotes: 6