0

I'm trying to open the new window in the Pyqt5. I did, I created a new window but the new window closes immediately. I tried a lot of ways but not work. It's my home task and I'm new in the PyQT5. How can I fix it?

import sys
from PyQt5 import QtCore, QtWidgets,QtGui
from PyQt5.QtWidgets import QMainWindow, QWidget, QPushButton, QAction,QApplication, QLabel, QVBoxLayout
from PyQt5.QtCore import QSize    
from PyQt5.QtGui import QIcon



class Doctors(QWidget):
    """
    This "window" is a QWidget. If it has no parent, it
    will appear as a free-floating window as we want.
    """
    def DoctorDeleteUpdate(self, Dialog):
        
        Dialog.setObjectName("Dialog")
        Dialog.resize(718, 614)
        self.verticalLayoutWidget = QtWidgets.QWidget(Dialog)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(20, 10, 661, 241))
        self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setObjectName("verticalLayout")
        self.treeWidget = QtWidgets.QTreeWidget(self.verticalLayoutWidget)
        self.treeWidget.setObjectName("treeWidget")
        self.treeWidget.headerItem().setText(0, "1")
        self.verticalLayout.addWidget(self.treeWidget)
        self.ID = QtWidgets.QLabel(Dialog)
        self.ID.setGeometry(QtCore.QRect(300, 270, 16, 21))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.ID.setFont(font)
        self.ID.setObjectName("ID")
        self.InputName = QtWidgets.QLineEdit(Dialog)
        self.InputName.setGeometry(QtCore.QRect(250, 300, 113, 20))
        self.InputName.setObjectName("InputName")
        self.InputSpecialty = QtWidgets.QLineEdit(Dialog)
        self.InputSpecialty.setGeometry(QtCore.QRect(250, 340, 113, 20))
        self.InputSpecialty.setObjectName("InputSpecialty")
        self.InputCost = QtWidgets.QLineEdit(Dialog)
        self.InputCost.setGeometry(QtCore.QRect(250, 380, 113, 20))
        self.InputCost.setObjectName("InputCost")
        self.InputReception = QtWidgets.QLineEdit(Dialog)
        self.InputReception.setGeometry(QtCore.QRect(250, 420, 113, 20))
        self.InputReception.setObjectName("InputReception")
        self.InputPercentage = QtWidgets.QLineEdit(Dialog)
        self.InputPercentage.setGeometry(QtCore.QRect(250, 460, 113, 20))
        self.InputPercentage.setObjectName("InputPercentage")
        self.label = QtWidgets.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(129, 300, 41, 13))
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(Dialog)
        self.label_2.setGeometry(QtCore.QRect(129, 340, 41, 13))
        self.label_2.setObjectName("label_2")
        self.label_3 = QtWidgets.QLabel(Dialog)
        self.label_3.setGeometry(QtCore.QRect(129, 380, 41, 13))
        self.label_3.setObjectName("label_3")
        self.label_4 = QtWidgets.QLabel(Dialog)
        self.label_4.setGeometry(QtCore.QRect(126, 420, 91, 20))
        self.label_4.setObjectName("label_4")
        self.label_5 = QtWidgets.QLabel(Dialog)
        self.label_5.setGeometry(QtCore.QRect(130, 460, 71, 16))
        self.label_5.setObjectName("label_5")
        self.ButtonSelect = QtWidgets.QPushButton(Dialog)
        self.ButtonSelect.setGeometry(QtCore.QRect(250, 490, 111, 23))
        self.ButtonSelect.setObjectName("ButtonSelect")
        self.ButtonUpdate = QtWidgets.QPushButton(Dialog)
        self.ButtonUpdate.setGeometry(QtCore.QRect(250, 520, 111, 23))
        self.ButtonUpdate.setObjectName("ButtonUpdate")
        self.ButtonDelete = QtWidgets.QPushButton(Dialog)
        self.ButtonDelete.setGeometry(QtCore.QRect(250, 550, 111, 23))
        self.ButtonDelete.setObjectName("ButtonDelete")

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.ID.setText(_translate("Dialog", "0"))
        self.label.setText(_translate("Dialog", "Name"))
        self.label_2.setText(_translate("Dialog", "Specialty"))
        self.label_3.setText(_translate("Dialog", "Cost"))
        self.label_4.setText(_translate("Dialog", "Reception Name"))
        self.label_5.setText(_translate("Dialog", "Percentage"))
        self.ButtonSelect.setText(_translate("Dialog", "Select"))
        self.ButtonUpdate.setText(_translate("Dialog", "Update"))
        self.ButtonDelete.setText(_translate("Dialog", "Delete"))

class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.setMinimumSize(QSize(600, 500))    
        self.setWindowTitle("Kharkiv National Hospital") 

        # Add button widget
        pybutton = QPushButton('Pyqt', self)
        pybutton.clicked.connect(self.clickMethod)
        pybutton.resize(100,32)
        pybutton.move(130, 30)        
        pybutton.setToolTip('This is a tooltip message.')  

        # Create new Doctor
        NewDoctor = QAction(QIcon('new.png'), '&New', self) 
        NewDoctor.setStatusTip('New Doctor')
        NewDoctor.triggered.connect(self.newCall)

        # Update & Delete Doctor
        UpdateDeleteceDoctor = QAction(QIcon('open.png'), '&Update / Delete', self)   
        UpdateDeleteceDoctor.setStatusTip('Update / Delete Doctors')
        UpdateDeleteceDoctor.triggered.connect(self.openCall)


        # Create menu bar and add action
        menuBar = self.menuBar()
        DoctorMenu = menuBar.addMenu('&Doctor')
        DoctorMenu.addAction(NewDoctor)
        DoctorMenu.addAction(UpdateDeleteceDoctor)

        # Create new Patient
        NewPatient = QAction(QIcon('new.png'), '&New', self) 
        NewPatient.setStatusTip('New Patient')
        NewPatient.triggered.connect(self.newCall)

        # Update & Delete Patients
        UpdateDeletecePatients = QAction(QIcon('open.png'), '&Update / Delete', self)   
        UpdateDeletecePatients.setStatusTip('New / Delete Patients')
        UpdateDeletecePatients.triggered.connect(self.openCall)


        # Create menu bar and add action        
        PatientMenu = menuBar.addMenu('&Patients')
        PatientMenu.addAction(NewPatient)
        PatientMenu.addAction(UpdateDeletecePatients)


        # Create new Inspection
        newInspection = QAction(QIcon('new.png'), '&New', self) 
        newInspection.setStatusTip('New Inspection')
        newInspection.triggered.connect(self.newCall)

        # Update & Delete Inspectopn
        UpdateDeleteInspection = QAction(QIcon('open.png'), '&Update / Delete', self)   
        UpdateDeleteInspection.setStatusTip('New / Delete Inspections')
        UpdateDeleteInspection.triggered.connect(self.openCall)


        # Create menu bar and add action        
        InspectionMenu = menuBar.addMenu('&Inspection')
        InspectionMenu.addAction(newInspection)
        InspectionMenu.addAction(UpdateDeleteInspection)


        
        # Export Doctor
        ExportDoctors = QAction(QIcon('new.png'), '&Doctor', self) 
        ExportDoctors.setStatusTip('Export Doctor')
        ExportDoctors.triggered.connect(self.newCall)

        # Export Patient
        ExportPatients = QAction(QIcon('open.png'), '&Export Patients', self)   
        ExportPatients.setStatusTip('Export Patients')
        ExportPatients.triggered.connect(self.openCall)


        # Create menu bar and add action        
        ExportMenu = menuBar.addMenu('&Export')
        ExportMenu.addAction(ExportDoctors)
        ExportMenu.addAction(ExportPatients)



        

    def openCall(self):
        print('Open')

    def newCall(self):
        nw = Doctors()
        Dialog = QtWidgets.QDialog()
        nw.DoctorDeleteUpdate(Dialog)
        nw.show()

    def exitCall(self):
        print('Exit app')

    def clickMethod(self):
        print('PyQt')

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    mainWin = MainWindow()
    mainWin.show()
    sys.exit( app.exec_() )

Console Output:

QT_DEVICE_PIXEL_RATIO is deprecated. 
Instead use:   QT_AUTO_SCREEN_SCALE_FACTOR to enable platform plugin controlled per-screen factors.
   QT_SCREEN_SCALE_FACTORS to set per-screen DPI.
   QT_SCALE_FACTOR to set the application global scale factor.
SiHa
  • 6,756
  • 12
  • 30
  • 41
Hamit
  • 15
  • 5

0 Answers0