如何獲取 Tkinter 中的螢幕大小?


Tkinter 最初建立一個視窗或框架物件,所有小部件、框架都在其中顯示。

Tkinter 元件根據使用者定義的幾何形狀調整視窗大小和寬度。

為了獲取螢幕大小,我們可以使用 winfo_screenwidth() 來返回螢幕的寬度,並使用 winfo_screenheight() 來返回螢幕的高度(以畫素為單位)。

示例

在此示例中,我們將列印螢幕大小作為輸出,

#Import the required libraries
from tkinter import *

#Create an instance of tkinter frame
win= Tk()

#Set the geometry of frame
win.geometry("650x250")

#Get the current screen width and height
screen_width = win.winfo_screenwidth()
screen_height = win.winfo_screenheight()

#Print the screen size
print("Screen width:", screen_width)
print("Screen height:", screen_height)

win.mainloop()

輸出

執行程式碼將顯示一個視窗並列印螢幕大小。

Screen width: 1366
Screen height: 768

更新於:2021 年 3 月 26 日

19K+ 瀏覽量

開啟你的 職業生涯

透過完成課程取得認證

開始
廣告