我有一个简单的脚本,该脚本会定期获取屏幕截图,并将其保存在共享文件夹中。我现在正在寻找一个脚本,该脚本可以截取该屏幕截图并将其显示在另一台PC上,并在其他脚本运行时更新显示的图片。我在网上找到了此代码,该代码可以正常工作,并显示在以下位置找到的第一张图片:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel
from PyQt5.QtGui import QIcon, QPixmap
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'My Screen'
self.left = 10
self.top = 10
self.width = 640
self.height = 480
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
label = QLabel(self)
pixmap = QPixmap('D:\\screen.png')
label.setPixmap(pixmap)
self.resize(pixmap.width(),pixmap.height())
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
我试图这样修改它,使其每60秒更新一次:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel
from PyQt5.QtGui import QIcon, QPixmap
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'My Screen'
self.left = 10
self.top = 10
self.width = 640
self.height = 480
self.initUI()
while True:
self.update_image()
time.sleep(60)
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
label = QLabel(self)
pixmap = QPixmap('D:\\screen.png')
label.setPixmap(pixmap)
self.resize(pixmap.width(),pixmap.height())
self.show()
def update_image(self):
label = QLabel(self)
pixmap = QPixmap('D:\\screen.png')
label.setPixmap(pixmap)
self.resize(pixmap.width(),pixmap.height())
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
但这显然是一个愚蠢的解决方案,它不起作用。你们中的任何一个都知道如何做吗?谢谢!
答案 0 :(得分:0)
如注释中所述,您必须使用QTimer
,另一方面,您要上传具有相同名称的文件,因此在这种情况下,最好重用QLabel
,因为否则应用程序将消耗大量内存。
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
path_of_image = 'D:\\screen.png'
class App(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.title = 'My Screen'
self.left = 10
self.top = 10
self.width = 640
self.height = 480
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.label = QtWidgets.QLabel(self)
timer = QtCore.QTimer(self)
timer.timeout.connect(self.update_image)
timer.start(60*1000)
self.update_image()
def update_image(self):
pixmap = QtGui.QPixmap(path_of_image)
if not pixmap.isNull():
self.label.setPixmap(pixmap)
self.label.adjustSize()
self.resize(pixmap.size())
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
ex = App()
ex.show()
sys.exit(app.exec_())