使用 Tkinter 获取屏幕的高度和宽度 | Python
Tkinter 提供了一些方法来帮助我们获取当前屏幕的高度和宽度。
以下方法可用于确定高度和宽度:
winfo_screenheight() // Returns screen height in pixels
winfo_screenmmheight() // Returns screen height in mm
winfo_screenwidth() // Returns screen width in pixels
winfo_screenmmwidth() // Returns screen width in mm
代码 #1:以像素为单位获取高度和宽度。
Python3
# importing tkinter module
from tkinter import * from tkinter.ttk import *
# creating tkinter window
root = Tk()
# getting screen's height in pixels
height = root.winfo_screenheight()
# getting screen's width in pixels
width = root.winfo_screenwidth()
print("\n width x height = %d x %d (in pixels)\n" %(width, height))
# infinite loop
mainloop()
Python3
# importing tkinter module
from tkinter import * from tkinter.ttk import *
# creating tkinter window
root = Tk()
# getting screen's height in mm
height = root.winfo_screenmmheight()
# getting screen's width in mm
width = root.winfo_screenmmwidth()
print("\n width x height = %d x %d (in mm)\n" %(width, height))
# infinite loop
mainloop()
输出:
代码 #2:以毫米为单位获取高度和宽度。
Python3
# importing tkinter module
from tkinter import * from tkinter.ttk import *
# creating tkinter window
root = Tk()
# getting screen's height in mm
height = root.winfo_screenmmheight()
# getting screen's width in mm
width = root.winfo_screenmmwidth()
print("\n width x height = %d x %d (in mm)\n" %(width, height))
# infinite loop
mainloop()
输出: