I have a main program window, in which there is a button, when it is pressed, another window should open. The problem is that when you press a button, the window opens and immediately closes. How to fix it?
Here is the code for the main window :
from PyQt5.QtWidgets import * from PyQt5.QtOpenGL import * from PyQt5.QtCore import * from PyQt5.QtGui import * from Config import * from ButWiz import * import sys class QtWinMaker(QtWinMakerConfig): def __init__(self, QtApp): super().__init__() self.initUI() self.show() self.QtAplication = QtApp def initAddButton(self): print("") def initUI(self): self.initWindow() print("InitUI") def initWindow(self): btn = QPushButton("Button", self) btn.move(100, 100) btn.clicked.connect(self.CallQtAddButton) self.setWindowTitle("ProgramTable") self.setGeometry(300, 300, 600, 600) def CallQtAddButton(self): ex = QtAddButton() ex.show() Second window code:
import sys from pathlib import Path from os.path import basename from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtCore import * class QtAddButton(QWidget): exe_path = "" image_path = "" def __init__(self): super().__init__() self.initUI() def initUI(self): self.setWindowTitle("Button wizard") self.setGeometry(200, 200, 250, 250) self.setFixedSize(250, 250) self.initLabels() self.initButtons() self.initFields() def initLabels(self): head = QLabel("Choose program and image:", self) head.move(50, 10) choose_exe_text = QLabel("Choose program: ", self) choose_exe_text.move(10, 83) choose_image_text = QLabel("Choose image:", self) choose_image_text.move(10, 113) set_name = QLabel("Enter name:", self) set_name.move(10, 53) def initButtons(self): self.choose_exe_btn = QPushButton("Choose pogram.. ", self) self.choose_exe_btn.move(100, 80) self.choose_exe_btn.clicked.connect(self.chooseProgram) self.choose_image_btn = QPushButton("Choose image.. ", self) self.choose_image_btn.move(100, 110) self.choose_image_btn.clicked.connect(self.chooseImage) self.confirm_btn = QPushButton("Confirm", self) self.confirm_btn.move(85, 220) self.confirm_btn.clicked.connect(self.confirmEvent) def initFields(self): self.name_field = QLineEdit(self) self.name_field.move(100, 53) def chooseProgram(self): file_dialog = QFileDialog.getOpenFileName(self, "Open...", filter="Executable files (*.exe)", initialFilter='Executable files (*.exe)') if file_dialog[0]!="": self.exe_path = file_dialog[0] self.choose_exe_btn.setText(basename(file_dialog[0])) def chooseImage(self): file_dialog = QFileDialog.getOpenFileName(self, "Open...", filter="Image files (*.png *.jpg *.jpeg *.gif)", initialFilter="Image files (*.png *.jpg *.jpeg *.gif)") if file_dialog[0] != "": self.image_path = file_dialog[0] self.choose_image_btn.setText(basename(file_dialog[0])) def confirmEvent(self): if Path('cnf.txt').is_file(): file = open("cnf.txt", "a") else: file = open("cnf.txt", "w") if self.name_field.text() != "": if self.exe_path != "": if self.image_path != "": file.write(self.name_field.text() + '\n' + self.exe_path + "\n" + self.image_path + "\n") file.close() self.close() else: QMessageBox.warning(self, "Warning", "Choose image.") else: QMessageBox.warning(self, "Warning", "Choose program.") else: QMessageBox.warning(self, "Warning", "Empty name field.") Main code
from UI import QtWinMaker import sys from PyQt5.QtWidgets import * from PyQt5.QtOpenGL import * from PyQt5.QtCore import * app = QApplication(sys.argv) ex = QtWinMaker(app) sys.exit(app.exec_())