我将这两个python脚本组合到一个窗口中。基本上,在选中按钮时将小部件添加到窗口。这是主窗口
from PyQt5 import QtCore, QtGui, QtWidgets
class MainWindow(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
self.checkBox = QtWidgets.QCheckBox(Form)
self.checkBox.setGeometry(QtCore.QRect(40, 40, 171, 21))
self.checkBox.setObjectName("checkBox")
// when checkbox is true add widget
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.checkBox.setText(_translate("Form", "render frame range"))
这是我要添加的小部件
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
self.lineEdit = QtWidgets.QLineEdit(Form)
self.lineEdit.setGeometry(QtCore.QRect(20, 120, 113, 20))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(Form)
self.lineEdit_2.setGeometry(QtCore.QRect(200, 120, 113, 20))
self.lineEdit_2.setObjectName("lineEdit_2")
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(140, 120, 47, 13))
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(30, 160, 131, 31))
self.pushButton.setObjectName("pushButton")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.label.setText(_translate("Form", "to"))
self.pushButton.setText(_translate("Form", "Render Sequence"))
我知道我需要将窗口小部件添加到主窗口,但我不知道如何。当复选框为true时,我希望添加小部件。如果复选框为false,我希望删除小部件。我见过用c ++但不是python的例子。如果有人可以帮助我或指出我正确的方向,这将是伟大的。
您无需添加/删除小部件。您可以根据复选框的状态隐藏或显示它。
将信号QCheckBox::clicked
连接到插槽QWidget::setVisible
:
class MainWindow(QWidget):
def __init__(self, parent=None, **kwargs):
super(MainWindow, self).__init__(parent, **kwargs)
self.resize(400, 300)
self.checkBox = QCheckBox("Hide/show", self)
self.checkBox.setGeometry(QRect(40, 40, 171, 21))
self.widgetToManage = QLabel("Hello world!", self)
self.widgetToManage.hide() # the checkbox is unchecked by default.
# When the checkbox is checked, the widget is visible.
self.checkBox.clicked.connect(self.widgetToManage.setVisible)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
app.setActiveWindow(window)
window.show()
sys.exit(app.exec_())