Search code examples
python-3.xuser-interfacepyqt5qspinbox

Spin box not showing up


I'm unsure why all aspects of my GUI are showing up apart from the spin box (the code for it is within the home function).

I've tried moving it to the init(self) function, but that doesn't work. I thought it would be intuitive for it to be within the home function as that is where all my other GUI (e.g. buttons) resides.

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui     import QIcon
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QAction, QMessageBox, QDoubleSpinBox

from temperature import MplWindow                     
from filament import MplWindow1
from highvoltage import MplWindow2

class window(QMainWindow):

    def __init__(self):
        super(window, self).__init__()
        self.setGeometry(50, 50, 300, 300)
        self.setWindowTitle('Temperature Control')
        self.setWindowIcon(QIcon('adn.png'))
        extractAction = QAction('&Quit', self)
        extractAction.setShortcut('Ctrl+Q')
        extractAction.setStatusTip('leave the app')
        extractAction.triggered.connect(self.close_application)
        self.statusBar()
        mainMenu = self.menuBar()
        fileMenu = mainMenu.addMenu('&File')
        fileMenu.addAction(extractAction)

        self.matplWindow = MplWindow()             
        self.matplWindow1 = MplWindow1()
        self.matplWindow2 = MplWindow2()

        self.home()


    def home(self):
        btn = QPushButton('quit', self)
        btn.clicked.connect(self.close_application)
        btn.resize(btn.sizeHint())
        btn.move(200, 260)

        button = QPushButton('Temperature',self)
        button.clicked.connect(self.opengraph)
        button.move(100,50)

        button = QPushButton('Filament voltage',self)
        button.clicked.connect(self.openfilament)
        button.move(100,80)

        button = QPushButton('High voltage',self)
        button.clicked.connect(self.openhigh)
        button.move(100,110)

        self.doubleSpinBox = QtWidgets.QDoubleSpinBox()
        self.doubleSpinBox.setGeometry(180, 110, 62, 22)

        self.show()

    def opengraph(self):
        self.matplWindow.funAnimation()              

    def openfilament(self):
        self.matplWindow1.funAnimation1()

    def openhigh(self):
        self.matplWindow2.funAnimation2()

    def close_application(self):
        choice = QMessageBox.question(self, 'Message',
                                     "Are you sure to quit?", QMessageBox.Yes |
                                     QMessageBox.No, QMessageBox.No)
        if choice == QMessageBox.Yes:
            sys.exit()
        else:
            pass

if __name__ == "__main__":
    app = QApplication(sys.argv)
    Gui = window()
    sys.exit(app.exec_())

Solution

  • I worked it out - I moved the code to the init function.

    import sys
    from PyQt5 import QtCore, QtGui, QtWidgets
    from PyQt5.QtGui     import QIcon
    from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QAction, QMessageBox, QDoubleSpinBox, QLabel, QVBoxLayout
    
    from temperature import MplWindow                     # +++
    from filament import MplWindow1
    from highvoltage import MplWindow2
    
    class window(QMainWindow):
    
        def __init__(self):
            super(window, self).__init__()
            self.setGeometry(50, 50, 300, 300)
            self.setWindowTitle('Temperature Control')
            self.setWindowIcon(QIcon('adn.png'))
            extractAction = QAction('&Quit', self)
            extractAction.setShortcut('Ctrl+Q')
            extractAction.setStatusTip('leave the app')
            extractAction.triggered.connect(self.close_application)
            self.statusBar()
            mainMenu = self.menuBar()
            fileMenu = mainMenu.addMenu('&File')
            fileMenu.addAction(extractAction)
    
            self.matplWindow = MplWindow()               # +++
            self.matplWindow1 = MplWindow1()
            self.matplWindow2 = MplWindow2()
    
    #        vBoxLayout = QVBoxLayout()
            self.label = QLabel("Set point Temp:", self)
            self.label.move(50,150)
    
            self.spinBox = QDoubleSpinBox(self)
            self.spinBox.move(70,150)
    
    
            self.home()
    
    
        def home(self):
            btn = QPushButton('quit', self)
            btn.clicked.connect(self.close_application)
            btn.resize(btn.sizeHint())
            btn.move(200, 260)
    
            button = QPushButton('Temperature',self)
            button.clicked.connect(self.opengraph)
            button.move(100,50)
    
            button = QPushButton('Filament voltage',self)
            button.clicked.connect(self.openfilament)
            button.move(100,80)
    
            button = QPushButton('High voltage',self)
            button.clicked.connect(self.openhigh)
            button.move(100,110)
    
            self.show()
    
        def opengraph(self):
            self.matplWindow.funAnimation()              # +++
    
        def openfilament(self):
            self.matplWindow1.funAnimation1()
    
        def openhigh(self):
            self.matplWindow2.funAnimation2()
    
        def close_application(self):
            choice = QMessageBox.question(self, 'Message',
                                         "Are you sure to quit?", QMessageBox.Yes |
                                         QMessageBox.No, QMessageBox.No)
            if choice == QMessageBox.Yes:
                sys.exit()
            else:
                pass
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        Gui = window()
        sys.exit(app.exec_())