I'm trying to learn how to use PyQt5 and PyOpenGL to do a simple gui for display some shapes. I searched a lot but i didnt find any example/tutorial that show me the basics. I just want to draw simple shapes in the widget of this mainwindow. Here is the code from the ui.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.openGLWidget = QtWidgets.QOpenGLWidget(self.centralwidget)
self.openGLWidget.setGeometry(QtCore.QRect(110, 50, 591, 451))
self.openGLWidget.setObjectName("openGLWidget")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Based on code from Trying to get PyQt5 and OpenGL working. I found it using Google.
You assign own method to self.openGLWidget.paintGL and draw OpenGL inside this method.
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.uic import *
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.openGLWidget = QtWidgets.QOpenGLWidget(self.centralwidget)
self.openGLWidget.setGeometry(QtCore.QRect(110, 50, 591, 451))
self.openGLWidget.setObjectName("openGLWidget")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.openGLWidget.initializeGL()
self.openGLWidget.resizeGL(651,551)
self.openGLWidget.paintGL = self.paintGL
timer = QTimer(self.centralwidget)
timer.timeout.connect(self.openGLWidget.update)
timer.start(1000)
def paintGL(self):
glClear(GL_COLOR_BUFFER_BIT)
glColor3f(1,0,0);
glBegin(GL_TRIANGLES);
glVertex3f(-0.5,-0.5,0);
glVertex3f(0.5,-0.5,0);
glVertex3f(0.0,0.5,0);
glEnd()
gluPerspective(45, 651/551, 0.1, 50.0)
glTranslatef(0.0,0.0, -5)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Related
I am trying to get the date from a calendar widget to get printed into a textbox. The calendar widget and the textbox are in the same class but the print function is in a different function. all in the same class.
here is the code:
from PyQt5 import QtCore, QtGui, QtWidgets
import calendar
I have the printDate function to print the date
class Ui_Calendar(object):
def printDate(self, qDate):
date =('{0}-{1}-{2}'.format(qDate.month(), qDate.day(), qDate.year()))
self.setupUi.textedit.setText(date)
Here in this function i have the calendar widget and the textbox:
def setupUi(self, MainWindow):
calendar widget:
self.calendarWidget = QtWidgets.QCalendarWidget(self.centralwidget)
self.calendarWidget.setGeometry(QtCore.QRect(0, 0, 392, 236))
self.calendarWidget.setObjectName("calendarWidget")
self.calendarWidget.clicked.connect(self.printDate)
textbox:
self.textedit = QtWidgets.QLineEdit(self.centralwidget)
self.textedit.setGeometry(QtCore.QRect(20,245,80,30))
self.textedit.setObjectName("textedit")
I get the calendar widget to open when button clicked. I also can get the date printed on python shell but cannot the date on the textbox. As soon as i click the date on the calendar widget the program closes itself. i dont get any error msg.
I am new to programming and just started learning. Please help me
here is my whole code:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Calendar(object):
def printDate(self, qDate):
date =('{0}-{1}-{2}'.format(qDate.month(), qDate.day(), qDate.year()))
#print(date)
self.setupUi.textedit.setText(self.date.toStrings())
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(395, 310)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.calendarWidget = QtWidgets.QCalendarWidget(self.centralwidget)
self.calendarWidget.setGeometry(QtCore.QRect(0, 0, 392, 236))
self.calendarWidget.setObjectName("calendarWidget")
#self.calendarWidget.clicked.connect(lambda dateval:print(dateval))
self.calendarWidget.clicked.connect(self.printDate)
MainWindow.setCentralWidget(self.centralwidget)
self.textedit = QtWidgets.QLineEdit(self.centralwidget)
self.textedit.setGeometry(QtCore.QRect(20,245,80,30))
self.textedit.setObjectName("textedit")
#self.printDate(self.label.setText(date))
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
#self.label.setText(_translate("Mainwindow","date"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_Calendar()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Do not modify the code generated by Qt Designer but create another class that inherits
from the appropriate widget and use the initial class to fill it.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Calendar(object):
# def printDate(self, qDate):
# date =('{0}-{1}-{2}'.format(qDate.month(), qDate.day(), qDate.year()))
# #print(date)
# self.setupUi.textedit.setText(self.date.toStrings())
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(395, 310)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.calendarWidget = QtWidgets.QCalendarWidget(self.centralwidget)
self.calendarWidget.setGeometry(QtCore.QRect(0, 0, 392, 236))
self.calendarWidget.setObjectName("calendarWidget")
#self.calendarWidget.clicked.connect(lambda dateval:print(dateval))
# self.calendarWidget.clicked.connect(self.printDate)
MainWindow.setCentralWidget(self.centralwidget)
self.textedit = QtWidgets.QLineEdit(self.centralwidget)
self.textedit.setGeometry(QtCore.QRect(20,245,80,30))
self.textedit.setObjectName("textedit")
#self.printDate(self.label.setText(date))
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
#self.label.setText(_translate("Mainwindow","date"))
class Calendar(QtWidgets.QMainWindow, Ui_Calendar):
def __init__(self):
super().__init__()
self.setupUi(self)
self.calendarWidget.clicked.connect(self.printDate)
def printDate(self, qDate):
date =('{0}-{1}-{2}'.format(qDate.month(), qDate.day(), qDate.year()))
# self.setupUi.textedit.setText(self.date.toStrings())
self.textedit.setText(date)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
# MainWindow = QtWidgets.QMainWindow()
# ui = Ui_Calendar()
# ui.setupUi(MainWindow)
# MainWindow.show()
w = Calendar()
w.show()
sys.exit(app.exec_())
In my PyQt5 Application I want to add an Import data window in order to make sure delimiter, skiprows, etc.. is correctly set before trying to import data from a file. Obviously I want this to happen in a separate window. I'm not going to post the full code there, just a quick example which is similar.
In this example there are two windows: the main and the import window. I'd like to send simple text from import window to mainwindow. My problem is I can't really access the created instance of MainWindow, because that happens in main.py.
Here is the code:
main.py
import sys
from logic import MainProgram
from PyQt5 import QtWidgets
def main():
app = QtWidgets.QApplication(sys.argv)
my_interface = MainProgram()
my_interface.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
logic.py
from PyQt5.QtWidgets import QFileDialog
from PyQt5 import QtWidgets, QtCore
from mainwindow_ui import Ui_MainWindow
from import_ui import Ui_ImportPage
class MainProgram(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainProgram, self).__init__(parent)
self.setupUi(self)
self.open.clicked.connect(self.open_import_page)
def open_import_page(self):
self.window1 = ImportPage(self)
self.window1.show()
class ImportPage(QtWidgets.QMainWindow, Ui_ImportPage):
def __init__(self, parent=None):
super(ImportPage, self).__init__(parent)
self.setupUi(self)
self.get.clicked.connect(self.get_text)
def get_text(self):
print(self.line.text()) # <--- this is want I want to send back to MainProgram class
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
my_interface = MainProgram()
my_interface.show()
sys.exit(app.exec_())
mainwindow_ui.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.open = QtWidgets.QPushButton(self.centralwidget)
self.open.setObjectName("open")
self.verticalLayout.addWidget(self.open)
self.line2 = QtWidgets.QLineEdit(self.centralwidget)
self.line2.setPlaceholderText('Recieved from import page')
self.verticalLayout.addWidget(self.line2)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.open.setText(_translate("MainWindow", "Open import page"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
import_ui.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_ImportPage(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("Import")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.get = QtWidgets.QPushButton('Send data to MainWindow', self.centralwidget)
self.verticalLayout.addWidget(self.get)
self.line = QtWidgets.QLineEdit(self.centralwidget)
self.line.setObjectName("open")
self.line.setPlaceholderText('Type here what you want to send to MainWindow')
self.verticalLayout.addWidget(self.line)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Import"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_ImportPage()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Should I change the structure of the code to achieve this? Is there anything I'm missing?
Thank you for answering.
You can access the ImportPage instance in MainProgram as you create it in that scope. So you should make the necessary connections and get the text:
class MainProgram(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainProgram, self).__init__(parent)
self.setupUi(self)
self.open.clicked.connect(self.open_import_page)
self.window1 = ImportPage(self)
self.window1.get.clicked.connect(self.on_clicked)
def open_import_page(self):
self.window1.show()
def on_clicked(self):
print(self.window1.line.text())
I Want this app to refresh the 'label' data (Which is a datetime) when i click the Refresh Button.
I tried pushButton.clicked.connect(ui.setupUi(MainWindow)), but it is not working, im out of clue how to do that.
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
import datetime
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(200, 200)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(30, 30, 121, 61))
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
import datetime
dt = str(datetime.datetime.now())
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", dt))
pushButton = QtWidgets.QPushButton(self.centralwidget)
pushButton.setGeometry(QtCore.QRect(120, 150, 75, 23))
pushButton.setObjectName("pushButton")
# pushButton.clicked.connect(ui.setupUi(MainWindow))
pushButton.setText(_translate("MainWindow", "Refresh"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
After clicking the Refresh button the time should update.
You really shouldn't re-initialize the ui for that, but just set the text for the label.
Also, since datetime.datetime.now() is "dynamic", you should use a lambda or, better, a dedicated slot.
The "simpler" solution, based on your code, is use this where you have the commented code:
pushButton.clicked.connect(lambda: self.label.setText(str(datetime.datetime.now())))
Or, sometimes better, the same thing in a specific method:
def retranslateUi(self, MainWindow):
# ...
pushButton.clicked.connect(self.setCurrentDate)
# ...
def setCurrentDate(self):
self.label.setText(str(datetime.datetime.now()))
You were there almost. Using pushButton.clicked.connect() is correct, but you don't need to recall the initUI function.
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
import datetime
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(200, 200)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(30, 30, 121, 61))
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
dt = str(datetime.datetime.now())
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", dt))
pushButton = QtWidgets.QPushButton(self.centralwidget)
pushButton.setGeometry(QtCore.QRect(120, 150, 75, 23))
pushButton.setObjectName("pushButton")
pushButton.clicked.connect(lambda: self.update_time())
pushButton.setText(_translate("MainWindow", "Refresh"))
def update_time(self):
self.label.setText(str(datetime.datetime.now()))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
I made it in a seperate function so you can add more things to it if you want. You could also make pushButton a class member so you can check the state in update_time(). Or you could pass it as an function argument.
I need to make my program remove Access-Control-Allow-Origin on any website it goes to, how can I do this? Here is the code I have right now:
from PyQt5 import QtCore, QtGui, QtWidgets, QtWebEngineWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.webView = QtWebEngineWidgets.QWebEngineView(self.centralwidget)
self.webView.setUrl(QtCore.QUrl("thewebsite"))
self.webView.loadFinished.connect(self.reform)
self.webView.setObjectName("webView")
self.gridLayout.addWidget(self.webView, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def reform(self, ok):
payload = open('payload.js', 'r').read()
if ok:
self.webView.page().runJavaScript('alert("Ready");')
self.webView.page().runJavaScript(payload)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
from PyQt5 import QtWebEngineWidgets
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.setFixedSize(500, 500)
MainWindow.show()
sys.exit(app.exec_())
How do I make it so it disables Access-Control-Allow-Origin?
You could try enabling the LocalContentCanAccessRemoteUrls WebAttribute:
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
settings = QtWebEngineWidgets.QWebEngineSettings.defaultSettings()
settings.setAttribute(QtWebEngineWidgets.QWebEngineSettings.LocalContentCanAccessRemoteUrls, True)
...
I have a simple pyqt UI in which there is a table widget. I want the widget to resize when I resize the main window. There is a similar question posted on stackOverflow (Resizing table columns when window is maximized), but the answers given do not solve my purpose. In particular I tried usingself.tableWidget.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
. It resizes the column width of the table according to the table widget geometry but does not resize the widget itself when I resize the main window. I'm using pyqt5 and spyder IDE.
Here is an example
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(566, 475)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget.setWindowTitle("Transactional Data")
self.tableWidget.setGeometry(QtCore.QRect(60, 30, 461, 381))
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(7)
self.tableWidget.setRowCount(5)
self.tableWidget.setAlternatingRowColors(True)
self.tableWidget.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 566, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
if __name__ == "__main__":
import sys
if not QtWidgets.QApplication.instance():
app = QtWidgets.QApplication(sys.argv)
else:
app = QtWidgets.QApplication.instance()
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Try it:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(566, 475)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.centralwidget)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.verticalLayout_2 = QtWidgets.QVBoxLayout()
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.horizontalLayout_2.addLayout(self.verticalLayout_2)
self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget.setObjectName("tableWidget")
self.verticalLayout_2.addWidget(self.tableWidget)
self.tableWidget.setWindowTitle("Transactional Data")
self.tableWidget.setColumnCount(7)
self.tableWidget.setRowCount(5)
self.tableWidget.setAlternatingRowColors(True)
self.tableWidget.setHorizontalHeaderLabels(['A','B','C','D','E','F','G'])
self.tableWidget.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 24))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())