如何使用PyQt5/PySide或任何其他Python库以全屏模式在辅助监视器上显示所需的图像?过去,我使用帧缓冲区图像查看器(Fbi和Fbi improved)。然而,这种方法要求我使用Linux。我更喜欢在Windows环境下工作,最好使用Python找到解决方案。在
动机/背景
我正在研究基于DLP投影的3D打印过程。当我使用HDMI将DLP投影仪连接到我的Windows PC时,它会显示为第二个监视器。我想将这个辅助监视器(DLP)专用于显示3D打印过程所需的图案图像(png、bmp或svg)。我想用Python编程控制显示哪个图像。 这是https://3dprinting.stackexchange.com/questions/1217/how-to-display-images-on-dlp-using-hdmi-for-3d-printing的后续问题
下面的代码是一个可能的解决方案,但是我不确定这是正确的还是最有效的方法。我发现使用PyQt5有两种方法:1)使用初始屏幕,2)使用QLabel。我的代码面临以下问题: 对于3D打印应用,解决方案需要满足以下要求:import time
start_time = time.time()
import sys
from PyQt5.QtWidgets import QApplication, QLabel, QSplashScreen
from PyQt5.QtGui import QPixmap, QCursor
from PyQt5.QtCore import Qt
import os
app = QApplication(sys.argv)
total_loops = 1
for i in range(total_loops):
# https://doc.qt.io/qtforpython/index.html
# https://www.riverbankcomputing.com/static/Docs/PyQt5/module_index.html
s = app.screens()[1] # Get the secondary screen
# Display info about secondary screen
print('Screen Name: {} Size: {}x{} Available geometry {}x{} '.format(s.name(), s.size().width(), s.size().height(), s.availableGeometry().width(), s.availableGeometry().height()))
# Hide cursor from appearing on screen
app.setOverrideCursor(QCursor(Qt.BlankCursor)) # https://forum.qt.io/topic/49877/hide-cursor
# Select desired image to be displayed
pixmap = QPixmap('test.png')
# Splash screen approach
# https://doc.qt.io/qtforpython/PySide2/QtWidgets/QSplashScreen.html?highlight=windowflags
splash = QSplashScreen(pixmap) # Set the splash screen to desired image
splash.show() # Show the splash screen
splash.windowHandle().setScreen(s) # Set splash screen to secondary monitor https://stackoverflow.com/a/30597458/4988010
splash.showFullScreen() # Show in splash screen in full screen mode
# # Qlabel apporach
# l = QLabel()
# l.setPixmap(pixmap)
# l.move(1920,0)
# l.show()
# l.windowHandle().setScreen(s) # https://stackoverflow.com/a/30597458/4988010
# l.showFullScreen()
time.sleep(0.5)
end_time = time.time()
print('Execution time: ', end_time-start_time )
sys.exit(app.exec_())
下面的代码是我问题的一个可能的解决方案。我的解决方案假设Qt只用于全屏显示图像,而不用于其余的逻辑。因此,我不得不run the QT app in a secondary thread。这是因为在我运行函数
app.exec_()
的那一刻,Qt将连续运行一个事件循环,从而阻塞了不依赖Qt的其余Python逻辑。据我所知,不建议在主线程之外运行QApplication
,因此我欢迎有经验的用户发布更好的方法。在我还要感谢@ekhurvo为我指出了QWidget属性/标志。在
您不应该在主线程之外运行GUI,因为Qt不能保证它按照the docs指示正确工作。您必须在另一个线程中执行其他繁重的任务,而不是在另一个线程中执行GUI。在
您必须改变传统顺序逻辑的方法,但必须使用面向事件的编程,在事件发生之前执行操作,在Qt through信号的情况下。在
考虑到上述情况,解决方案是:
相关问题 更多 >
编程相关推荐