我有这两个 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"))
我知道我需要将小部件作为主窗口的父级,但我不知道如何。当复选框为真时,我希望添加小部件。如果复选框为 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_())
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句