簡體   English   中英

如何在 Tkinter 中獲取屏幕尺寸?

[英]How to get the screen size in Tkinter?

我想知道是否可以使用 tkinter 計算屏幕尺寸。

我想要這個,這樣可以讓程序在屏幕中央打開......

import tkinter as tk

root = tk.Tk()

screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()

一個可能的解決方案

import os

os.system("xrandr  | grep \* | cut -d' ' -f4")

我的輸出:

1440x900
0

對於窗戶:

您可以讓進程知道 dpi 以處理縮放顯示。

import ctypes
try: # >= win 8.1
    ctypes.windll.shcore.SetProcessDpiAwareness(2)
except Exception as e:
    pass
try: # < win 8.1
    ctypes.windll.user32.SetProcessDPIAware()
except: # win 8.0 or less
    pass

擴展@mouad 的答案,此函數能夠處理多顯示器並返回當前屏幕的分辨率:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM