I'm trying to create a simple app with 5 functions each accessed from a button on the main page.
The App class looks like this
import sys
from PyQt5 import QtGui, QtWidgets, QtCore
from PyQt5.QtWidgets import QApplication
class App(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self._main = QtWidgets.QWidget()
self.initUI_M()
def initUI_M(self):
self.setWindowTitle("QC Tool")
self.setWindowIcon(QtGui.QIcon('pythonlogo.png'))
layout =QtWidgets.QVBoxLayout( )
button1 = QtWidgets.QPushButton("GPS")
button1.clicked.connect(partial(self.plotFlight_map))
layout.addWidget(button1)
button2 = QtWidgets.QPushButton("XY")
button2.clicked.connect(partial(self.plot_xy))
layout.addWidget(button2)
button3 = QtWidgets.QPushButton("Accel")
button3.clicked.connect(partial(self.plot_accel))
layout.addWidget(button3)
button4 = QtWidgets.QPushButton("Gyro")
button4.clicked.connect(partial(self.plot_gyro))
layout.addWidget(button4)
button5 = QtWidgets.QPushButton("Altimeter")
button5.clicked.connect(partial(self.plot_Altimeter))
layout.addWidget(button5)
self._main.setLayout(layout)
self.center()
self.show()
This is what I get on running...
I've tried setting self._main, or just self as the parent on construction of the buttons and the QVBoxLayout, and I've tried not assigning methods to the buttons. All of the methods connected to the clicked.connect exist and were previously connected to a menu that worked.
I've even tried not creating the widget object and just adding the layout to self.
I get the exact same output no matter what I try.
I know it's a syntax issue, but I can't find it...
You forgot to set the main widget of the QMainWindow. You've created a QWidget object called "_main", but you still need to set it as the central widget of your main window.
You need to set the main widget using the setCentralWidget method of QMainWindow.
def initUI_M(self):
self.setWindowTitle("QC Tool")
self.setWindowIcon(QtGui.QIcon('pythonlogo.png'))
central_widget = QtWidgets.QWidget() # Add This to your code
self.setCentralWidget(central_widget) # Add This to your code
layout = QtWidgets.QVBoxLayout(central_widget)
Then all you need to do is add the following functions:
self.center()
self.show()
def plotFlight_map(self): # Add This to your code
print("GPS")
def plot_xy(self): # Add This to your code
print("XY")
def plot_accel(self): # Add This to your code
print("Accel")
def plot_gyro(self): # Add This to your code
print("Gyro")
def plot_Altimeter(self): # Add This to your code
print("Altimeter")
def center(self): # Add This to your code
qr = self.frameGeometry()
cp = QtWidgets.QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
if __name__ == '__main__': # Add This to your code
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())