我正在使用Python和PyQt5创建一个程序。这是我第一次使用PyQt。
实际上,当您启动程序时,会出现主窗口。然后,用户可以在两个选项之间进行选择,由两个按钮显示。
当用户点击按钮选择一个选项时,会打开一个新的子窗口,用户必须填写其中的一些字段
我的问题是,我的儿童窗口每次打开后都会关闭。
这是我的代码:
main.py
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QGridLayout, QFormLayout, QGroupBox, QLabel, QLineEdit,
QComboBox, QTextEdit, QPushButton, QTreeView, QMenuBar, QMainWindow
from PyQt5.QtGui import QStandardItemModel
from scelle_window import ScelleWindow
from oe_window import OeWindow
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('''Main Window''')
self.resize(800, 500)
main_widget = QWidget()
self.setCentralWidget(main_widget)
grid = QGridLayout()
bt_add_scelle = QPushButton('''Add objetc''')
bt_add_scelle.clicked.connect(self.add_scelle_dial)
bt_add_oe = QPushButton('''Add desc''')
bt_add_oe.clicked.connect(self.add_oe_dial)
grpbox_tree = QGroupBox('''List''')
layout_tree = QGridLayout()
layout_tree.addWidget(case_tree, 0, 0)
layout_tree.addWidget(bt_add_scelle, 1, 0)
layout_tree.addWidget(bt_add_oe, 2, 0)
grpbox_tree.setLayout(layout_tree)
grid.addWidget(grpbox_tree, 3, 0, 1, 2)
main_widget.setLayout(grid)
def add_scelle_dial(self):
desc_scelle = ScelleWindow()
desc_scelle.show()
def add_oe_dial(self):
desc_oe = OeWindow()
desc_oe.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
win = MainWindow()
win.show()
sys.exit(app.exec_())
scelle_window
from PyQt5.QtWidgets import QWidget, QFormLayout, QLabel, QLineEdit, QComboBox
class ScelleWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('''Object informations''')
self.resize(500, 300)
# ... following code...
oe_window
from PyQt5.QtWidgets import QWidget, QFormLayout, QLabel, QLineEdit, QComboBox
class OeWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('''Object description''')
self.resize(500, 300)
# ... following code...
这是因为收集碳水化合物还是其他原因?
感谢
是的,您需要保留对小部件的引用。使小部件的成员变量将做的技巧:
def add_scelle_dial(self):
self.desc_scelle = ScelleWindow()
self.desc_scelle.show()
def add_oe_dial(self):
self.desc_oe = OeWindow()
self.desc_oe.show()