问题描述
一个外部模块启动了这个 QDialog 窗口,我想在其中添加标签、按钮和图像。在这个新窗口中,我正在为两个因素苦苦挣扎:1)它在按钮显示之前就点击了。 .addLayouts,图像总是出现在窗口的左上角(或在 .move 位置),而不是像预期的那样堆叠在 QHBoxLayouts 和 QVBoxLayouts 中。
外部模块调用窗口对象(见下面屏幕截图中的箭头):
def photoButtonPressed(self):
print("Photo button pressed")
photoWidget = FieldPhotos(self.photoFP,self.photoDT,self)
photoWidget.show()
首先,我注释掉所有与像素图/图像相关的内容以对按钮进行故障排除,并且代码会在甚至显示窗口之前自动运行 updateImagePressed 方法(传递方向变量“next”)(更不用说单击下一步按钮) :
class FieldPhotos(QtWidgets.QDialog):
def __init__(self,photoPath,photoDT,parent=None):
# Paths and DTs are lists of photos/datetimes within the time limit
super().__init__(parent)
self.setModal(True)
self.photoPath = photoPath
self.photoDT = photoDT
self.left = 300
self.top = 300
self.width = 800
self.height = 1000
self.imageSelect = 0
self.initUI()
def initUI(self):
self.setWindowTitle(f'{self.photoDT[0]} {os.path.split(self.photoPath[self.imageSelect])[-1]}')
self.setGeometry(self.left,self.top,self.width,self.height)
# Create widgets ############################
# self.labelImage = QtWidgets.QLabel(self)
labelNote = QtWidgets.QLabel('Close this window to continue.',self)
listLabel = QtWidgets.QLabel(f'Number of images found within 90 mins of data: {len(self.photoPath)} ',self)
self.nextButton = QtWidgets.QPushButton('>')
self.nextButton.setToolTip('Next image in list')
self.previousButton = QtWidgets.QPushButton('<')
self.previousButton.setToolTip('Previous image in list')
self.nextButton.clicked.connect(self.updateImagePressed('next'))
self.previousButton.clicked.connect(self.updateImagePressed('previous'))
# Setup Layout ##################################
self.VBox = QtWidgets.QVBoxLayout()
# self.VBox.addWidget(labelImage)
self.VBox.addWidget(labelNote)
self.HBox = QtWidgets.QHBoxLayout()
self.HBox.addWidget(listLabel)
self.HBox.addWidget(self.previousButton)
self.HBox.addWidget(self.nextButton)
self.VBox.addLayout(self.HBox)
# # Set initial photo
# self.pixmap = QPixmap(self.photoPath[self.imageSelect])
# self.pixmapScaled = self.pixmap.scaled(800,800,QtCore.Qt.KeepAspectRatio)
# self.labelImage.setPixmap(self.pixmapScaled)
# self.resize(self.pixmapScaled.width(),self.pixmapScaled.height())
# self.labelImage.move(0,100) # Since it won't work in VBox,move down to make space?
# self.updateImagePressed('first')
def updateImagePressed(self,direction):
print(f'Display {direction} image: {self.photoPath[self.imageSelect]}')
if self.imageSelect ==0:
self.previousButton.setDisabled(1)
if self.imageSelect == len(self.photoPath):
self.nextButton.setDisabled(0)
if direction == 'next':
self.imageSelect += 1
if direction == 'previous':
self.imageSelect -= 1
# Set photo
# self.pixmap = QPixmap(self.photoPath[self.imageSelect])
# self.pixmapScaled = self.pixmap.scaled(800,100)
结果如下:
Photo button pressed
Display next image: /Users/daurin/Projects_Supplemental/HyperPACE/field_data/HyperSAS/VIIRS2019/Photos/IMG_20190908_104845.jpg
Traceback (most recent call last):
File "/Users/daurin/GitRepos/HyperInSPACE/Source/AnomalyDetection.py",line 356,in photoButtonPressed
photoWidget = FieldPhotos(self.photoFP,self)
File "/Users/daurin/GitRepos/HyperInSPACE/Source/FieldPhotos.py",line 28,in __init__
self.initUI()
File "/Users/daurin/GitRepos/HyperInSPACE/Source/FieldPhotos.py",line 45,in initUI
self.nextButton.clicked.connect(self.updateImagePressed('next'))
TypeError: argument 1 has unexpected type 'NoneType'"
接下来,当我注释掉按钮动作时,并像这样恢复 QPixmap 代码:
def initUI(self):
self.setWindowTitle(f'{self.photoDT[0]} {os.path.split(self.photoPath[self.imageSelect])[-1]}')
self.setGeometry(self.left,self.height)
# Create widgets ############################
self.labelImage = QtWidgets.QLabel(self)
labelNote = QtWidgets.QLabel('Close this window to continue.',self)
self.nextButton = QtWidgets.QPushButton('>')
self.nextButton.setToolTip('Next image in list')
self.previousButton = QtWidgets.QPushButton('<')
self.previousButton.setToolTip('Previous image in list')
# self.nextButton.clicked.connect(self.updateImagePressed('next'))
# self.previousButton.clicked.connect(self.updateImagePressed('previous'))
# Setup Layout ##################################
self.VBox = QtWidgets.QVBoxLayout()
self.VBox.addWidget(self.labelImage)
self.VBox.addWidget(labelNote)
self.HBox = QtWidgets.QHBoxLayout()
self.HBox.addWidget(listLabel)
self.HBox.addWidget(self.previousButton)
self.HBox.addWidget(self.nextButton)
self.VBox.addLayout(self.HBox)
# Set initial photo
self.pixmap = QPixmap(self.photoPath[self.imageSelect])
self.pixmapScaled = self.pixmap.scaled(800,QtCore.Qt.KeepAspectRatio)
self.labelImage.setPixmap(self.pixmapScaled)
self.resize(self.pixmapScaled.width(),self.pixmapScaled.height())
self.labelImage.move(0,move down to make space?
# self.updateImagePressed('first')
它忽略了所有的 VBoxes 和 HBoxes,只在左上角堆积内容而忽略按钮:
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)