升级的小部件不会出现在使用qt designer创建的主应用程序中
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了升级的小部件不会出现在使用qt designer创建的主应用程序中相关的知识,希望对你有一定的参考价值。
我正在编写一个由几个独立小部件组成的应用程序,以使实验运行。例如,目的是控制不同的仪器(例如,示波器,照相机或机械快门)。
我在Linux上使用Qt Designer和PyQt5创建的每个独立小部件都可以正常工作。我的想法是现在将这些小部件嵌入QMainWindow中。到目前为止,我所做的(并且运行良好)如下:
- 在QTabWidget中,我将单个窗口小部件放置在一个标签内(三个不同的标签,所以三个不同的窗口小部件)。我将每个小部件提升为一个控制范围(或摄像机)的类。通过运行主应用程序,自定义窗口小部件可以正确显示,并且所有内容均按预期运行。
然后,我在QGroupBox对象内创建相同类型的自定义小部件,并希望将其升级到与以前使用的类不同的类。推广小部件的步骤相同。现在,通过运行主应用程序,占位符中什么都没有显示。
在与主窗口相对应的.py文件中,与我希望将小部件升级为的类相对应的所有文件都已导入。
所以我的问题是:您对发生的事情有任何想法吗?
我尝试在Internet上找到一些答案,看起来它与.qrc文件和资源有关。但这远远超出了我对PyQt和Qt Designer的了解。任何帮助将不胜感激!
谢谢!
编辑
这是我的最小可重现性示例,由一组一个工作小部件和一个不可见部件组成。
main_viewer.py:
import numpy as np
import sys
import os
import time
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import pyqtSlot
from ui_main import Ui_MainViewer
from camera_viewer import CameraViewer
from laser import LaserViewer
class MainViewer(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainViewer, self).__init__(parent)
ui = Ui_MainViewer()
ui.setupUi(self)
self.main_ui = ui
# Redefinition of items in the first tab to keep only one
self.main_ui.andor_widget.ui.ui_camera_cmb.clear()
self.main_ui.andor_widget.ui.ui_camera_cmb.addItems(["Camera #1"])
self.main_ui.basler_widget.ui.ui_camera_cmb.clear()
self.main_ui.basler_widget.ui.ui_camera_cmb.addItems(["Camera #2"])
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = MainViewer()
window.show()
sys.exit(app.exec_())
ui_main.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'ui_main.ui'
#
# Created by: PyQt5 UI code generator 5.9.2
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainViewer(object):
def setupUi(self, MainViewer):
MainViewer.setObjectName("MainViewer")
MainViewer.resize(1337, 778)
self.centralwidget = QtWidgets.QWidget(MainViewer)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.light_sources_groupBox = QtWidgets.QGroupBox(self.centralwidget)
self.light_sources_groupBox.setObjectName("light_sources_groupBox")
self.gridLayout_2 = QtWidgets.QGridLayout(self.light_sources_groupBox)
self.gridLayout_2.setObjectName("gridLayout_2")
self.red_laser_groupBox = QtWidgets.QGroupBox(self.light_sources_groupBox)
self.red_laser_groupBox.setObjectName("red_laser_groupBox")
self.gridLayout_5 = QtWidgets.QGridLayout(self.red_laser_groupBox)
self.gridLayout_5.setObjectName("gridLayout_5")
self.widget = LaserViewer(self.red_laser_groupBox)
self.widget.setObjectName("widget")
self.gridLayout_5.addWidget(self.widget, 0, 0, 1, 1)
self.gridLayout_2.addWidget(self.red_laser_groupBox, 0, 0, 1, 1)
self.green_laser_groupBox = QtWidgets.QGroupBox(self.light_sources_groupBox)
self.green_laser_groupBox.setObjectName("green_laser_groupBox")
self.gridLayout_4 = QtWidgets.QGridLayout(self.green_laser_groupBox)
self.gridLayout_4.setObjectName("gridLayout_4")
self.widget_2 = QtWidgets.QWidget(self.green_laser_groupBox)
self.widget_2.setObjectName("widget_2")
self.gridLayout_4.addWidget(self.widget_2, 0, 0, 1, 1)
self.gridLayout_2.addWidget(self.green_laser_groupBox, 0, 1, 1, 1)
self.white_light_groupBox = QtWidgets.QGroupBox(self.light_sources_groupBox)
self.white_light_groupBox.setObjectName("white_light_groupBox")
self.gridLayout_15 = QtWidgets.QGridLayout(self.white_light_groupBox)
self.gridLayout_15.setObjectName("gridLayout_15")
self.widget_3 = QtWidgets.QWidget(self.white_light_groupBox)
self.widget_3.setObjectName("widget_3")
self.gridLayout_15.addWidget(self.widget_3, 0, 0, 1, 1)
self.gridLayout_2.addWidget(self.white_light_groupBox, 0, 2, 1, 1)
self.gridLayout.addWidget(self.light_sources_groupBox, 2, 0, 1, 1)
self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox.setObjectName("groupBox")
self.gridLayout_3 = QtWidgets.QGridLayout(self.groupBox)
self.gridLayout_3.setObjectName("gridLayout_3")
self.tabWidget = QtWidgets.QTabWidget(self.groupBox)
self.tabWidget.setObjectName("tabWidget")
self.tab = QtWidgets.QWidget()
self.tab.setObjectName("tab")
self.gridLayout_13 = QtWidgets.QGridLayout(self.tab)
self.gridLayout_13.setObjectName("gridLayout_13")
self.andor_widget = CameraViewer(self.tab)
self.andor_widget.setObjectName("andor_widget")
self.gridLayout_13.addWidget(self.andor_widget, 0, 0, 1, 1)
self.tabWidget.addTab(self.tab, "")
self.tab_2 = QtWidgets.QWidget()
self.tab_2.setObjectName("tab_2")
self.gridLayout_14 = QtWidgets.QGridLayout(self.tab_2)
self.gridLayout_14.setObjectName("gridLayout_14")
self.basler_widget = CameraViewer(self.tab_2)
self.basler_widget.setObjectName("basler_widget")
self.gridLayout_14.addWidget(self.basler_widget, 0, 0, 1, 1)
self.tabWidget.addTab(self.tab_2, "")
self.tab_3 = QtWidgets.QWidget()
self.tab_3.setObjectName("tab_3")
self.scope_widget = QtWidgets.QWidget(self.tab_3)
self.scope_widget.setGeometry(QtCore.QRect(9, 9, 741, 631))
self.scope_widget.setObjectName("scope_widget")
self.tabWidget.addTab(self.tab_3, "")
self.gridLayout_3.addWidget(self.tabWidget, 0, 0, 1, 1)
self.gridLayout.addWidget(self.groupBox, 0, 1, 4, 1)
self.groupBox_7 = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox_7.setObjectName("groupBox_7")
self.gridLayout_8 = QtWidgets.QGridLayout(self.groupBox_7)
self.gridLayout_8.setObjectName("gridLayout_8")
self.groupBox_10 = QtWidgets.QGroupBox(self.groupBox_7)
self.groupBox_10.setObjectName("groupBox_10")
self.gridLayout_9 = QtWidgets.QGridLayout(self.groupBox_10)
self.gridLayout_9.setObjectName("gridLayout_9")
self.horizontalLayout_5 = QtWidgets.QHBoxLayout()
self.horizontalLayout_5.setObjectName("horizontalLayout_5")
self.pushButton_5 = QtWidgets.QPushButton(self.groupBox_10)
self.pushButton_5.setObjectName("pushButton_5")
self.horizontalLayout_5.addWidget(self.pushButton_5)
self.pushButton_6 = QtWidgets.QPushButton(self.groupBox_10)
self.pushButton_6.setObjectName("pushButton_6")
self.horizontalLayout_5.addWidget(self.pushButton_6)
self.gridLayout_9.addLayout(self.horizontalLayout_5, 0, 0, 1, 1)
self.gridLayout_8.addWidget(self.groupBox_10, 0, 1, 1, 1)
self.groupBox_8 = QtWidgets.QGroupBox(self.groupBox_7)
self.groupBox_8.setObjectName("groupBox_8")
self.gridLayout_10 = QtWidgets.QGridLayout(self.groupBox_8)
self.gridLayout_10.setObjectName("gridLayout_10")
self.widget_4 = QtWidgets.QWidget(self.groupBox_8)
self.widget_4.setObjectName("widget_4")
self.gridLayout_10.addWidget(self.widget_4, 0, 0, 1, 1)
self.gridLayout_8.addWidget(self.groupBox_8, 0, 0, 1, 1)
self.gridLayout.addWidget(self.groupBox_7, 3, 0, 1, 1)
self.groupBox_2 = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox_2.setObjectName("groupBox_2")
self.horizontalLayout_8 = QtWidgets.QHBoxLayout(self.groupBox_2)
self.horizontalLayout_8.setObjectName("horizontalLayout_8")
self.widget_5 = QtWidgets.QWidget(self.groupBox_2)
self.widget_5.setObjectName("widget_5")
self.horizontalLayout_8.addWidget(self.widget_5)
self.gridLayout.addWidget(self.groupBox_2, 0, 0, 2, 1)
MainViewer.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainViewer)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1337, 20))
self.menubar.setObjectName("menubar")
MainViewer.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainViewer)
self.statusbar.setObjectName("statusbar")
MainViewer.setStatusBar(self.statusbar)
self.retranslateUi(MainViewer)
self.tabWidget.setCurrentIndex(2)
QtCore.QMetaObject.connectSlotsByName(MainViewer)
def retranslateUi(self, MainViewer):
_translate = QtCore.QCoreApplication.translate
MainViewer.setWindowTitle(_translate("MainViewer", "Superfluidity experiment"))
self.light_sources_groupBox.setTitle(_translate("MainViewer", "Light sources"))
self.red_laser_groupBox.setTitle(_translate("MainViewer", "Red laser"))
self.green_laser_groupBox.setTitle(_translate("MainViewer", "Green laser"))
self.white_light_groupBox.setTitle(_translate("MainViewer", "White light"))
self.groupBox.setTitle(_translate("MainViewer", "Cameras and Scope"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainViewer", "Camera Andor"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainViewer", "Camera Basler"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_3), _translate("MainViewer", "Scope"))
self.groupBox_7.setTitle(_translate("MainViewer", "Aqcuisition"))
self.groupBox_10.setTitle(_translate("MainViewer", "Images"))
self.pushButton_5.setText(_translate("MainViewer", "Acquire"))
self.pushButton_6.setText(_translate("MainViewer", "Save data"))
self.groupBox_8.setTitle(_translate("MainViewer", "Filters"))
self.groupBox_2.setTitle(_translate("MainViewer", "SLM and DMD"))
from camera_viewer import CameraViewer
from laser import LaserViewer
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainViewer = QtWidgets.QMainWindow()
ui = Ui_MainViewer()
ui.setupUi(MainViewer)
MainViewer.show()
sys.exit(app.exec_())
camera_viewer.py(运行良好的小部件)
import numpy as np
import sys
import time
from PIL import Image
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import pyqtSlot
import pyqtgraph as pg
from ui_camera import Ui_CameraViewer
# to avoid pyqtgraph to crash
MIN_DISPLAY_TIME = 0.03
MAX_FRAME_RATE = 5.
class CameraViewer(QtWidgets.QWidget):
def __init__(self, parent=None):
super(CameraViewer, self).__init__(parent)
ui = Ui_CameraViewer()
ui.setupUi(self)
self.ui = ui
# Connect all ui buttons and initialise default values
self.ui.ui_exposure_time_dspn.setValue(MIN_DISPLAY_TIME)
self.ui.ui_frame_rate_dspn.setValue(MAX_FRAME_RATE)
self.ui.ui_camera_cmb.addItems(["Camera #1", "Camera #2"])
#TODO make a method for that
self.ui.ui_live_btn.setDisabled(True)
self.ui.ui_stop_live_btn.setDisabled(True)
self.ui.ui_snapshot_btn.setDisabled(True)
self.ui.ui_exposure_time_dspn.setDisabled(True)
self.ui.ui_frame_rate_dspn.setDisabled(True)
self.ui.ui_exposure_time_label.setDisabled(True)
self.ui.ui_frame_rate_label.setDisabled(True)
self.ui.ui_adjust_level_btn.setDisabled(True)
self.ui.ui_auto_level_chk.setDisabled(True)
self.ui.ui_crosshair_chk.setDisabled(True)
self.ui.ui_save_btn.setDisabled(True)
self.ui.ui_save_params_btn.setDisabled(True)
self.ui.ui_load_params_btn.setDisabled(True)
# Design of the image display
# Image
self.plot_image = self.ui.ui_image.addPlot(name='image', labels={'left': "<span style='color: red'>Y</span> (px)", 'bottom': "<span style='color: yellow'>X</span> (px)"}, colspan=3)
self.plot_image.setMinimumHeight(200)
self.view = self.plot_image.vb
self.view.setAspectLocked(True)
self.image = pg.ImageItem(border='w')
self.plot_image.addItem(self.image)
# X and Y profiles
self.ui.ui_image.nextRow()
self.plot_profiles = self.ui.ui_image.addPlot(labels={'left': 'Pixel value', 'bottom': "<span style='color: yellow'>X</span>, <span style='color: red'>Y</span> (px)"}, colspan=3)
self.plot_profiles.setMaximumHeight(75)
self.plot_profiles.setXLink('image')
# Image informations
self.ui.ui_image.nextRow()
self.label_prof = self.ui.ui_image.addLabel(justify='left', col=1)
self.label_prof.setMaximumHeight(25)
self.label_hist = self.ui.ui_image.addLabel(justify='left', col=2)
self.label_prof.setMaximumHeight(25)
# Crosshair
self.vLine = pg.InfiniteLine(angle=90, movable=False)
self.hLine = pg.InfiniteLine(angle=0, movable=False)
self.index_x = 0
self.index_y = 0
self.cam_connected = False
self.mode_live = False
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = CameraViewer()
window.show()
sys.exit(app.exec_())
# main()
ui_camera.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'ui_camera.ui'
#
# Created by: PyQt5 UI code generator 5.9.2
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_CameraViewer(object):
def setupUi(self, CameraViewer):
CameraViewer.setObjectName("CameraViewer")
CameraViewer.resize(590, 756)
self.gridLayout_4 = QtWidgets.QGridLayout(CameraViewer)
self.gridLayout_4.setObjectName("gridLayout_4")
self.frame_3 = QtWidgets.QFrame(CameraViewer)
self.frame_3.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame_3.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame_3.setObjectName("frame_3")
self.gridLayout_3 = QtWidgets.QGridLayout(self.frame_3)
self.gridLayout_3.setObjectName("gridLayout_3")
self.ui_image = GraphicsLayoutWidget(self.frame_3)
self.ui_image.setObjectName("ui_image")
self.gridLayout_3.addWidget(self.ui_image, 0, 0, 1, 1)
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.ui_adjust_level_btn = QtWidgets.QPushButton(self.frame_3)
self.ui_adjust_level_btn.setObjectName("ui_adjust_level_btn")
self.horizontalLayout_2.addWidget(self.ui_adjust_level_btn)
self.ui_auto_level_chk = QtWidgets.QCheckBox(self.frame_3)
self.ui_auto_level_chk.setChecked(True)
self.ui_auto_level_chk.setObjectName("ui_auto_level_chk")
self.horizontalLayout_2.addWidget(self.ui_auto_level_chk)
self.ui_crosshair_chk = QtWidgets.QCheckBox(self.frame_3)
self.ui_crosshair_chk.setObjectName("ui_crosshair_chk")
self.horizontalLayout_2.addWidget(self.ui_crosshair_chk)
self.gridLayout_3.addLayout(self.horizontalLayout_2, 1, 0, 1, 1)
self.gridLayout_4.addWidget(self.frame_3, 1, 0, 1, 1)
self.frame_2 = QtWidgets.QFrame(CameraViewer)
self.frame_2.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame_2.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame_2.setObjectName("frame_2")
self.gridLayout_2 = QtWidgets.QGridLayout(self.frame_2)
self.gridLayout_2.setObjectName("gridLayout_2")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.ui_save_btn = QtWidgets.QPushButton(self.frame_2)
self.ui_save_btn.setObjectName("ui_save_btn")
self.horizontalLayout.addWidget(self.ui_save_btn)
self.ui_save_params_btn = QtWidgets.QPushButton(self.frame_2)
self.ui_save_params_btn.setObjectName("ui_save_params_btn")
self.horizontalLayout.addWidget(self.ui_save_params_btn)
self.ui_load_params_btn = QtWidgets.QPushButton(self.frame_2)
self.ui_load_params_btn.setObjectName("ui_load_params_btn")
self.horizontalLayout.addWidget(self.ui_load_params_btn)
self.gridLayout_2.addLayout(self.horizontalLayout, 4, 0, 1, 2)
self.gridLayout_4.addWidget(self.frame_2, 2, 0, 1, 1)
self.frame = QtWidgets.QFrame(CameraViewer)
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.gridLayout = QtWidgets.QGridLayout(self.frame)
self.gridLayout.setObjectName("gridLayout")
self.ui_live_btn = QtWidgets.QPushButton(self.frame)
self.ui_live_btn.setObjectName("ui_live_btn")
self.gridLayout.addWidget(self.ui_live_btn, 3, 0, 1, 1)
self.ui_snapshot_btn = QtWidgets.QPushButton(self.frame)
self.ui_snapshot_btn.setObjectName("ui_snapshot_btn")
self.gridLayout.addWidget(self.ui_snapshot_btn, 3, 2, 1, 1)
self.ui_stop_live_btn = QtWidgets.QPushButton(self.frame)
self.ui_stop_live_btn.setEnabled(False)
self.ui_stop_live_btn.setObjectName("ui_stop_live_btn")
self.gridLayout.addWidget(self.ui_stop_live_btn, 3, 1, 1, 1)
self.horizontalLayout_4 = QtWidgets.QHBoxLayout()
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
self.ui_exposure_time_label = QtWidgets.QLabel(self.frame)
self.ui_exposure_time_label.setObjectName("ui_exposure_time_label")
self.horizontalLayout_4.addWidget(self.ui_exposure_time_label)
self.ui_exposure_time_dspn = QtWidgets.QDoubleSpinBox(self.frame)
self.ui_exposure_time_dspn.setDecimals(3)
self.ui_exposure_time_dspn.setMinimum(0.03)
self.ui_exposure_time_dspn.setMaximum(10000.0)
self.ui_exposure_time_dspn.setSingleStep(0.01)
self.ui_exposure_time_dspn.setProperty("value", 0.05)
self.ui_exposure_time_dspn.setObjectName("ui_exposure_time_dspn")
self.horizontalLayout_4.addWidget(self.ui_exposure_time_dspn)
self.gridLayout.addLayout(self.horizontalLayout_4, 4, 0, 1, 1)
self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.ui_frame_rate_label = QtWidgets.QLabel(self.frame)
self.ui_frame_rate_label.setEnabled(True)
self.ui_frame_rate_label.setObjectName("ui_frame_rate_label")
self.horizontalLayout_3.addWidget(self.ui_frame_rate_label)
self.ui_frame_rate_dspn = QtWidgets.QDoubleSpinBox(self.frame)
self.ui_frame_rate_dspn.setMinimum(0.0)
self.ui_frame_rate_dspn.setMaximum(30.0)
self.ui_frame_rate_dspn.setSingleStep(0.1)
self.ui_frame_rate_dspn.setProperty("value", 10.0)
self.ui_frame_rate_dspn.setObjectName("ui_frame_rate_dspn")
self.horizontalLayout_3.addWidget(self.ui_frame_rate_dspn)
self.gridLayout.addLayout(self.horizontalLayout_3, 4, 1, 1, 2)
self.ui_disconnect_btn = QtWidgets.QPushButton(self.frame)
self.ui_disconnect_btn.setObjectName("ui_disconnect_btn")
self.gridLayout.addWidget(self.ui_disconnect_btn, 2, 2, 1, 1)
self.ui_camera_cmb = QtWidgets.QComboBox(self.frame)
self.ui_camera_cmb.setObjectName("ui_camera_cmb")
self.gridLayout.addWidget(self.ui_camera_cmb, 2, 0, 1, 1)
self.ui_connect_btn = QtWidgets.QPushButton(self.frame)
self.ui_connect_btn.setObjectName("ui_connect_btn")
self.gridLayout.addWidget(self.ui_connect_btn, 2, 1, 1, 1)
self.gridLayout_4.addWidget(self.frame, 0, 0, 1, 1)
self.retranslateUi(CameraViewer)
QtCore.QMetaObject.connectSlotsByName(CameraViewer)
def retranslateUi(self, CameraViewer):
_translate = QtCore.QCoreApplication.translate
CameraViewer.setWindowTitle(_translate("CameraViewer", "Form"))
self.ui_adjust_level_btn.setText(_translate("CameraViewer", "Ajust level"))
self.ui_auto_level_chk.setText(_translate("CameraViewer", "Auto level"))
self.ui_crosshair_chk.setText(_translate("CameraViewer", "Crosshair"))
self.ui_save_btn.setText(_translate("CameraViewer", "Save image"))
self.ui_save_params_btn.setText(_translate("CameraViewer", "Save Params"))
self.ui_load_params_btn.setText(_translate("CameraViewer", "Load Params"))
self.ui_live_btn.setText(_translate("CameraViewer", "Live mode"))
self.ui_snapshot_btn.setText(_translate("CameraViewer", "Snapshot"))
self.ui_stop_live_btn.setText(_translate("CameraViewer", "Stop live mode"))
self.ui_exposure_time_label.setText(_translate("CameraViewer", "Exposure time (s)"))
self.ui_frame_rate_label.setText(_translate("CameraViewer", "Frame rate (Hz)"))
self.ui_disconnect_btn.setText(_translate("CameraViewer", "Disconnect"))
self.ui_connect_btn.setText(_translate("CameraViewer", "Connect"))
from pyqtgraph import GraphicsLayoutWidget
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
CameraViewer = QtWidgets.QWidget()
ui = Ui_CameraViewer()
ui.setupUi(CameraViewer)
CameraViewer.show()
sys.exit(app.exec_())
laser.py(无效的小部件)
import sys
import serial
import random
import struct
import numpy as np
from PyQt5 import QtCore, QtGui, QtWidgets
from ui_laser import Ui_Laser
class LaserViewer(QtWidgets.QDialog):
def __init__(self, parent=None):
super(LaserViewer, self).__init__(parent)
# set up of the ui
self.ui_laser = Ui_Laser()
self.ui_laser.setupUi(self)
# Initialization
self.ui_laser.ui_wavelength_cmb.addItems(["Green", "Red"])
self.ui_laser.pwr_doubleSpinBox.value()
self.picoscope_connected = False
self.laser_id = 'green'
self.ui_laser.laser_on_pushButton.setDisabled(True)
self.ui_laser.laser_off_pushButton.setDisabled(True)
self.ui_laser.pwr_doubleSpinBox.setDisabled(True)
self.ui_laser.power_value_lineEdit.setDisabled(True)
self.ui_laser.continuous_slow_right_pushButton.setDisabled(True)
self.ui_laser.continuous_slow_left_pushButton.setDisabled(True)
self.ui_laser.laser_disconnect_pushButton.setDisabled(True)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window1 = LaserViewer()
window1.show()
sys.exit(app.exec_())
# main()
ui_laser.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'ui_laser.ui'
#
# Created by: PyQt5 UI code generator 5.9.2
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Laser(object):
def setupUi(self, Laser):
Laser.setObjectName("Laser")
Laser.resize(559, 75)
self.gridLayout_3 = QtWidgets.QGridLayout(Laser)
self.gridLayout_3.setObjectName("gridLayout_3")
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setObjectName("gridLayout")
self.laser_off_pushButton = QtWidgets.QPushButton(Laser)
self.laser_off_pushButton.setObjectName("laser_off_pushButton")
self.gridLayout.addWidget(self.laser_off_pushButton, 1, 1, 1, 1)
self.pwr_doubleSpinBox = QtWidgets.QDoubleSpinBox(Laser)
self.pwr_doubleSpinBox.setObjectName("pwr_doubleSpinBox")
self.gridLayout.addWidget(self.pwr_doubleSpinBox, 0, 4, 1, 1)
self.ui_wavelength_cmb = QtWidgets.QComboBox(Laser)
self.ui_wavelength_cmb.setObjectName("ui_wavelength_cmb")
self.gridLayout.addWidget(self.ui_wavelength_cmb, 0, 0, 1, 1)
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.gridLayout.addItem(spacerItem, 1, 2, 1, 1)
self.laser_on_pushButton = QtWidgets.QPushButton(Laser)
self.laser_on_pushButton.setObjectName("laser_on_pushButton")
self.gridLayout.addWidget(self.laser_on_pushButton, 0, 1, 1, 1)
self.label_5 = QtWidgets.QLabel(Laser)
self.label_5.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
self.label_5.setObjectName("label_5")
self.gridLayout.addWidget(self.label_5, 0, 3, 1, 1)
self.label_6 = QtWidgets.QLabel(Laser)
self.label_6.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
self.label_6.setObjectName("label_6")
self.gridLayout.addWidget(self.label_6, 1, 3, 1, 1)
self.power_value_lineEdit = QtWidgets.QLineEdit(Laser)
self.power_value_lineEdit.setReadOnly(True)
self.power_value_lineEdit.setObjectName("power_value_lineEdit")
self.gridLayout.addWidget(self.power_value_lineEdit, 1, 4, 1, 1)
spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.gridLayout.addItem(spacerItem1, 0, 2, 1, 1)
self.label = QtWidgets.QLabel(Laser)
self.label.setObjectName("label")
self.gridLayout.addWidget(self.label, 0, 5, 1, 1)
self.label_2 = QtWidgets.QLabel(Laser)
self.label_2.setObjectName("label_2")
self.gridLayout.addWidget(self.label_2, 1, 5, 1, 1)
self.continuous_slow_right_pushButton = QtWidgets.QPushButton(Laser)
self.continuous_slow_right_pushButton.setMaximumSize(QtCore.QSize(22, 22))
self.continuous_slow_right_pushButton.setObjectName("continuous_slow_right_pushButton")
self.gridLayout.addWidget(self.continuous_slow_right_pushButton, 0, 7, 1, 1)
self.continuous_slow_left_pushButton = QtWidgets.QPushButton(Laser)
self.continuous_slow_left_pushButton.setMaximumSize(QtCore.QSize(22, 22))
self.continuous_slow_left_pushButton.setObjectName("continuous_slow_left_pushButton")
self.gridLayout.addWidget(self.continuous_slow_left_pushButton, 0, 6, 1, 1)
self.gridLayout_2 = QtWidgets.QGridLayout()
self.gridLayout_2.setObjectName("gridLayout_2")
self.laser_connect_pushButton = QtWidgets.QPushButton(Laser)
self.laser_connect_pushButton.setObjectName("laser_connect_pushButton")
self.gridLayout_2.addWidget(self.laser_connect_pushButton, 0, 0, 1, 1)
self.laser_disconnect_pushButton = QtWidgets.QPushButton(Laser)
self.laser_disconnect_pushButton.setObjectName("laser_disconnect_pushButton")
self.gridLayout_2.addWidget(self.laser_disconnect_pushButton, 0, 1, 1, 1)
self.gridLayout.addLayout(self.gridLayout_2, 1, 0, 1, 1)
self.gridLayout_3.addLayout(self.gridLayout, 0, 0, 1, 1)
self.retranslateUi(Laser)
QtCore.QMetaObject.connectSlotsByName(Laser)
def retranslateUi(self, Laser):
_translate = QtCore.QCoreApplication.translate
Laser.setWindowTitle(_translate("Laser", "Laser control"))
self.laser_off_pushButton.setText(_translate("Laser", "Close shutter"))
self.laser_on_pushButton.setText(_translate("Laser", "Open shutter"))
self.label_5.setText(_translate("Laser", "Power setting"))
self.label_6.setText(_translate("Laser", "Power on PD"))
self.label.setText(_translate("Laser", "µW"))
self.label_2.setText(_translate("Laser", "µW"))
self.continuous_slow_right_pushButton.setText(_translate("Laser", ">"))
self.continuous_slow_left_pushButton.setText(_translate("Laser", "<"))
self.laser_connect_pushButton.setText(_translate("Laser", "Connect"))
self.laser_disconnect_pushButton.setText(_translate("Laser", "Disconnect"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Laser = QtWidgets.QWidget()
ui = Ui_Laser()
ui.setupUi(Laser)
Laser.show()
sys.exit(app.exec_())
感谢您的帮助,对于问题的长度感到抱歉...
如bu Heike所指出,问题来自LaserViewer
的父类。通过从QWidget
而不是QDialog
继承,可以解决此问题。
非常感谢您指出这一点!
以上是关于升级的小部件不会出现在使用qt designer创建的主应用程序中的主要内容,如果未能解决你的问题,请参考以下文章
使用 Python 为 Qt Designer 自定义 Qt 小部件
使用 PyQt5 在 Qt Designer 中的自定义(升级)小部件中获取另一个小部件的当前值