python获取显示器大小
时间: 2023-10-16 16:15:31 浏览: 57
您好,可以使用 Python 的 tkinter 模块来获取显示器的大小,具体代码如下:
```
import tkinter as tk
root = tk.Tk()
width = root.winfo_screenwidth()
height = root.winfo_screenheight()
print("显示器大小为:{}x{}".format(width, height))
```
注意,这种方法获取的显示器大小仅适用于 Windows 和 Linux 平台,不适用于 macOS。
相关问题
python获取屏幕大小
你可以使用 Python 中的 `screeninfo` 模块来获取屏幕的大小。首先,你需要安装这个模块,可以使用以下命令来安装:
```shell
pip install screeninfo
```
安装完成后,你可以使用以下代码来获取屏幕的大小:
```python
from screeninfo import get_monitors
# 获取屏幕大小
screen = get_monitors()[0]
width, height = screen.width, screen.height
print(f"屏幕宽度: {width}px")
print(f"屏幕高度: {height}px")
```
这样,你就可以获取到屏幕的宽度和高度。请注意,如果你有多个显示器,上述代码只能获取到主显示器的大小。如果你需要获取所有显示器的大小,你可以遍历 `get_monitors()` 返回的列表。
qt获取显示器鼠标的位置
可以使用QCursor类的pos()方法获取当前鼠标位置,使用QApplication类的desktop()方法获取当前显示器的大小和位置,然后再根据鼠标位置判断当前鼠标在哪个显示器中。
示例代码:
```python
from PyQt5.QtWidgets import QApplication
from PyQt5.QtGui import QCursor
app = QApplication([])
cursor_pos = QCursor.pos()
# 获取所有显示器的信息
desktop = QApplication.desktop()
for i in range(desktop.screenCount()):
screen_geometry = desktop.screenGeometry(i)
if screen_geometry.contains(cursor_pos):
print("Mouse is in screen #{}".format(i))
print("Screen geometry:", screen_geometry)
break
```
注意,如果你的应用程序支持多个显示器,那么你需要在窗口初始化时设置窗口所在的显示器,以确保窗口出现在正确的位置。可以使用QDesktopWidget类的availableGeometry()方法获取指定显示器的大小和位置。
示例代码:
```python
from PyQt5.QtGui import QGuiApplication, QScreen
from PyQt5.QtWidgets import QMainWindow, QDesktopWidget
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setGeometry(0, 0, 640, 480)
self.move_to_screen(1) # 将窗口移动到第二个显示器
def move_to_screen(self, screen_index):
screen = QGuiApplication.screens()[screen_index]
available_geometry = QDesktopWidget().availableGeometry(screen)
self.move(available_geometry.topLeft())
app = QApplication([])
main_window = MainWindow()
main_window.show()
app.exec_()
```