如何解决如何在PyQt5的GUI中创建多个QLabel?
我在PyCharm中使用PyQt5创建了一个gui,并且设法显示了其中一个带有图像(Picture1.png)的QLabel,但是,当我尝试添加带有第二个图像的第二个QLabel(在同一个窗口上名为Shutter1.png的文件),似乎同时删除了两个标签,并且GUI上没有任何显示。我不确定我要去哪里出错,对您的任何帮助将不胜感激,我是新手!注意:我已经仔细检查了imagePath和imagePath_1的文件路径是否正确。参见下面的附加代码:
from PyQt5 import uic,QtWidgets,QtGui,QtCore
import sys
import pkg_resources
import functions.initialisation as inits
import functions.Sig2Open as S2O
import functions.Sig2Close as S2C
class Ui(QtWidgets.QMainWindow):
def __init__(self):
super(Ui,self).__init__()
self.gui = uic.loadUi('Shuttergui.ui',self)
# Creates the path of the image
self.imagePath = "C:/........../Picture1.png"
self.label = QtWidgets.QLabel(self.gui)
self.image = QtGui.QImage(self.imagePath)
self.pixmapImage = QtGui.QPixmap.fromImage(self.image)
self.label.setPixmap(self.pixmapImage)
self.label.resize(self.width(),self.height())
self.label.move(60,170)
self.imagePath = "C:/....../Shutter1.png"
# Create label that holds the image in imagePath
self.label_1 = QtWidgets.QLabel(self.gui)
self.image_1 = QtGui.QImage(self.imagePath)
self.pixmapImage_1 = QtGui.QPixmap.fromImage(self.image_1)
self.label_1.setPixmap(self.pixmapImage_1)
self.label_1.resize(self.width(),self.height())
self.label_1.move(60,170)
self.gui.showMaximized()
# redirect closeevent func to main self rather than inside gui
self.gui.closeEvent = self.closeEvent
# Initialise shutter functions
inits.ardopenup(self)
inits.ardshutup(self)
self.gui.show()
def closeEvent(self,event):
import time
time.sleep(0.1)
print("main thread quitting")
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
app.setStyleSheet(pkg_resources.resource_stream(__name__,'/css/darktheme/style.css').read().decode())
window = Ui()
sys.exit(app.exec_())
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。