Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีดึงขนาดหน้าจอใน Tkinter


ภาพรวม

Tkinter สร้าง Window/Frame Object ที่แสดง Widgets, Frames Components ปรับขนาดหน้าต่างตาม User-defined Geometry เพื่อดึงขนาดหน้าจอ ใช้ winfo_screenwidth() คืนความกว้างหน้าจอ และ winfo_screenheight() ความสูงใน Pixels

ตัวอย่างโค้ด

# 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()

ผลลัพธ์

รันโค้ดจะแสดงหน้าต่างและ Print ขนาดหน้าจอ

Screen width: 1366
Screen height: 768