如何在 Tkinter 中获取屏幕尺寸?
Tkinter最初创建一个窗口或框架对象,其中显示所有小部件、框架。
Tkinter组件根据用户定义的几何形状调整窗口大小和宽度。
为了获得屏幕尺寸,我们可以使用winfo_screenwidth()which返回屏幕宽度和屏幕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) print("屏幕高度:", screen_height) win.mainloop()输出结果
运行代码将显示一个窗口并打印屏幕大小。
屏幕宽度: 1366 屏幕高度: 768