.UI converted to .Py falls in PyInstaller application - python

I try to release python application, that uses Qt gui. To deploy app in fast and python-free way i used Pyinstaller
I converted my UI to py.
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(362, 669)
self.centralwidget = QtWidgets.QWidget(MainWindow)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(1)
sizePolicy.setVerticalStretch(1)
sizePolicy.setHeightForWidth(self.centralwidget.sizePolicy().hasHeightForWidth())
self.centralwidget.setSizePolicy(sizePolicy)
self.centralwidget.setAutoFillBackground(True)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(0, 0, 361, 671))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setSizeConstraint(QtWidgets.QLayout.SetMaximumSize)
self.verticalLayout.setContentsMargins(2, 2, 2, 2)
self.verticalLayout.setObjectName("verticalLayout")
self.verticalLayout_2 = QtWidgets.QVBoxLayout()
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.pshBtnSetInput = QtWidgets.QPushButton(self.verticalLayoutWidget)
self.pshBtnSetInput.setObjectName("pshBtnSetInput")
self.horizontalLayout.addWidget(self.pshBtnSetInput)
self.lineEdit = QtWidgets.QLineEdit(self.verticalLayoutWidget)
self.lineEdit.setObjectName("lineEdit")
self.horizontalLayout.addWidget(self.lineEdit)
self.verticalLayout_2.addLayout(self.horizontalLayout)
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.pshBtnSetOutput = QtWidgets.QPushButton(self.verticalLayoutWidget)
self.pshBtnSetOutput.setObjectName("pshBtnSetOutput")
self.horizontalLayout_2.addWidget(self.pshBtnSetOutput)
self.lineEdit_2 = QtWidgets.QLineEdit(self.verticalLayoutWidget)
self.lineEdit_2.setObjectName("lineEdit_2")
self.horizontalLayout_2.addWidget(self.lineEdit_2)
self.verticalLayout_2.addLayout(self.horizontalLayout_2)
self.verticalLayout.addLayout(self.verticalLayout_2)
self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.pshBtnAddAttributes = QtWidgets.QPushButton(self.verticalLayoutWidget)
self.pshBtnAddAttributes.setObjectName("pshBtnAddAttributes")
self.horizontalLayout_3.addWidget(self.pshBtnAddAttributes)
self.pshBtnDelAttribute = QtWidgets.QPushButton(self.verticalLayoutWidget)
self.pshBtnDelAttribute.setObjectName("pshBtnDelAttribute")
self.horizontalLayout_3.addWidget(self.pshBtnDelAttribute)
self.verticalLayout.addLayout(self.horizontalLayout_3)
self.tableView = QtWidgets.QTableView(self.verticalLayoutWidget)
self.tableView.setObjectName("tableView")
self.verticalLayout.addWidget(self.tableView)
self.pshBtnCut = QtWidgets.QPushButton(self.verticalLayoutWidget)
self.pshBtnCut.setObjectName("pshBtnCut")
self.verticalLayout.addWidget(self.pshBtnCut)
MainWindow.setCentralWidget(self.centralwidget)
self.actionOpen = QtWidgets.QAction(MainWindow)
self.actionOpen.setObjectName("actionOpen")
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Нарезка тайлов"))
self.pshBtnSetInput.setText(_translate("MainWindow", "Входящие"))
self.pshBtnSetOutput.setText(_translate("MainWindow", "Исходящие"))
self.pshBtnAddAttributes.setText(_translate("MainWindow", "Добавить атрибут"))
self.pshBtnDelAttribute.setText(_translate("MainWindow", "Удалить атрибут"))
self.pshBtnCut.setText(_translate("MainWindow", "Нарезать"))
self.actionOpen.setText(_translate("MainWindow", "Open Folder"))
It all works fine in interpreter, it works fine when executed from .py or when i test from spyder. I frozed it with pyinstaller, and it works.
But when i deploy it on any win7 x86 machines it stops working first time i call ui class.
I traced error to call of any member of this class inside the constructor of MainWindow.
class MainForm(QtWidgets.QMainWindow):
"""
Main Form of RasterCutterForm
"""
def __init__(self, parent = None):
try:
QtWidgets.QWidget.__init__(self,parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.DataModel = None
self.DataModel = QtGui.QStandardItemModel(parent = self)
self.DataModel.setHorizontalHeaderLabels(["Атрибут", "Значение"])
print("self.tableView.setModel(self.DataModel)")
self.ui.tableView.setModel(self.DataModel)
print("Init Done.")
except Exception as exc:
print(exc)
Last print( " Set Model " ) is the last executed command.
Any ideas?

Related

Trio + PyQT5 in one program?

How can I use trio in conjunction with PyQT5? The thing is that my program has interface written using PyQT5, and now I need to run eventloop trio, to work with the network, because I use trio WebSocket to connect to the server. I read that I should use trio.lowlevel.start_guest_run for this purpose. But in the documentation it says that in addition to the trio function I must pass run_sync_soon_threadsafe and done_callback as arguments. The documentation gives an example with asyncio and says that I have to define similar functions for my event_loop, in my case for PyQT. Unfortunately my knowledge is not enough to do it myself.
I wrote a very simple application using PyQT and put in the body of the class an asynchronous function that if it works correctly should change the inscription on the timer every second. In addition you can enter text in the input box and by pressing the button this text will be displayed at the bottom. I did not run the asynchronous function, as that is my question. At best, I expect the answer to my question to be a modified program in which the asynchronous function and the PyQT5 components run in the same thread using trio. Thank you for your reply.
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
import trio
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(804, 595)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(10, 10, 781, 591))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem)
self.lineEdit = QtWidgets.QLineEdit(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setPointSize(20)
self.lineEdit.setFont(font)
self.lineEdit.setObjectName("lineEdit")
self.verticalLayout.addWidget(self.lineEdit)
self.pushButton = QtWidgets.QPushButton(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setPointSize(20)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
self.verticalLayout.addWidget(self.pushButton)
self.label = QtWidgets.QLabel(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setPointSize(20)
self.label.setFont(font)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.verticalLayout.addWidget(self.label)
spacerItem1 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem1)
self.label_2 = QtWidgets.QLabel(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setPointSize(20)
self.label_2.setFont(font)
self.label_2.setAlignment(QtCore.Qt.AlignCenter)
self.label_2.setObjectName("label_2")
self.verticalLayout.addWidget(self.label_2)
spacerItem2 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem2)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Pushme"))
self.label.setText(_translate("MainWindow", ""))
self.label_2.setText(_translate("MainWindow", "Time"))
class PushMe(Ui_MainWindow):
def __init__(self, MainWindow):
super(PushMe, self).__init__()
self.setupUi(MainWindow)
self.PushMe = MainWindow
self.lineEdit.setPlaceholderText("type something")
self.connect_button()
# self.timer()
def connect_button(self):
self.pushButton.clicked.connect(self.set_text)
def set_text(self):
self.label.setText(self.lineEdit.text())
async def timer(self):
a = 1
while True:
self.label_2.setText(str(a))
a += 1
await trio.sleep(1)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = PushMe(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
I have to admit that I haven't been keeping up on maintenance on QTrio, but it does mix Qt with Trio. https://qtrio.readthedocs.io/en/stable/ Depending what route you want to go you could either use it as a whole library, or you could just pick out the pieces you want.
https://github.com/altendky/qtrio/blob/43c7ff24c0be2f7a3df86eef3c6cc5dae2f7ffd3/qtrio/_core.py#L649-L657
trio.lowlevel.start_guest_run(
self.trio_main,
async_fn,
args,
run_sync_soon_threadsafe=self.run_sync_soon_threadsafe,
done_callback=self.trio_done,
clock=self.clock, # type: ignore[arg-type]
instruments=self.instruments,
)
https://github.com/altendky/qtrio/blob/43c7ff24c0be2f7a3df86eef3c6cc5dae2f7ffd3/qtrio/_core.py#L672-L683
def run_sync_soon_threadsafe(self, fn: typing.Callable[[], object]) -> None:
"""Helper for the Trio guest to execute a sync function in the Qt host
thread when called from the Trio guest thread. This call will not block waiting
for completion of ``fn`` nor will it return the result of calling ``fn``.
Args:
fn: A no parameter callable.
"""
import qtrio.qt
event = qtrio.qt.ReenterEvent(fn=fn)
self.application.postEvent(self.reenter, event)
Most likely the easiest way is to use qtrio.Runner. Below I have run an asynchronous function using this class by adding just 2 lines of code to an already completed application. In my case this is the answer to my question.
# -*- coding: utf-8 -*-
import qtrio
from PyQt5 import QtCore, QtGui, QtWidgets
import trio
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(804, 595)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(10, 10, 781, 591))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem)
self.lineEdit = QtWidgets.QLineEdit(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setPointSize(20)
self.lineEdit.setFont(font)
self.lineEdit.setObjectName("lineEdit")
self.verticalLayout.addWidget(self.lineEdit)
self.pushButton = QtWidgets.QPushButton(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setPointSize(20)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
self.verticalLayout.addWidget(self.pushButton)
self.label = QtWidgets.QLabel(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setPointSize(20)
self.label.setFont(font)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.verticalLayout.addWidget(self.label)
spacerItem1 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem1)
self.label_2 = QtWidgets.QLabel(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setPointSize(20)
self.label_2.setFont(font)
self.label_2.setAlignment(QtCore.Qt.AlignCenter)
self.label_2.setObjectName("label_2")
self.verticalLayout.addWidget(self.label_2)
spacerItem2 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem2)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Pushme"))
self.label.setText(_translate("MainWindow", ""))
self.label_2.setText(_translate("MainWindow", "Time"))
class PushMe(Ui_MainWindow):
def __init__(self, MainWindow):
super(PushMe, self).__init__()
self.setupUi(MainWindow)
self.PushMe = MainWindow
self.lineEdit.setPlaceholderText("type something")
self.connect_button()
# self.timer()
def connect_button(self):
self.pushButton.clicked.connect(self.set_text)
def set_text(self):
self.label.setText(self.lineEdit.text())
async def timer(self):
a = 1
while True:
self.label_2.setText(str(a))
a += 1
await trio.sleep(1)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = PushMe(MainWindow)
MainWindow.show()
trio_loop = qtrio.Runner(application=app)
trio_loop.run(ui.timer)
sys.exit(app.exec_())

Change main window active tab from a dialogbox action

I have two windows in 2 separate classes. The main window and a dialogbox window. The main window contains a Qtabwidget with multiple tabs. The dialogbox has a button.
From the main window, on the click of a button the dialogbox opens. The user performs some action and clicks the save button. On the click of the button I would like the dialogbox to close, and the mainwindow to change from the initial tab to the second tab.
My problem is that I cannot get the tabwidget to change tabs on the dialogbox save button click signal.
Attempt:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(392, 138)
self.gridLayout = QtWidgets.QGridLayout(Dialog)
self.gridLayout.setObjectName("gridLayout")
self.frame = QtWidgets.QFrame(Dialog)
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.frame)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.label = QtWidgets.QLabel(self.frame)
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
self.verticalLayout.addWidget(self.label)
self.lineEdit_vehicleName = QtWidgets.QLineEdit(self.frame)
self.lineEdit_vehicleName.setObjectName("lineEdit_vehicleName")
self.verticalLayout.addWidget(self.lineEdit_vehicleName)
spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem)
self.verticalLayout_2.addLayout(self.verticalLayout)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem1)
self.pushButton_save = QtWidgets.QPushButton(self.frame)
self.pushButton_save.setObjectName("pushButton_save")
self.horizontalLayout.addWidget(self.pushButton_save)
self.pushButton_cancel = QtWidgets.QPushButton(self.frame)
self.pushButton_cancel.setObjectName("pushButton_cancel")
self.horizontalLayout.addWidget(self.pushButton_cancel)
self.verticalLayout_2.addLayout(self.horizontalLayout)
self.gridLayout.addWidget(self.frame, 0, 0, 1, 1)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
# Dialog = QtWidgets.QDialog()
self.pushButton_save.clicked.connect(Dialog.close)
self.pushButton_save.clicked.connect(Ui_MainWindow.changeTab)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.label.setText(_translate("Dialog", "Enter Name:"))
self.pushButton_save.setText(_translate("Dialog", "Save"))
self.pushButton_cancel.setText(_translate("Dialog", "Cancel"))
class Ui_MainWindow(object):
def changeTab(self):
self.tabWidget.setCurrentIndex(1)
def openDialog(self):
self.window = QtWidgets.QDialog()
self.ui = Ui_Dialog()
self.ui.setupUi(self.window)
self.window.show()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(548, 399)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
self.tabWidget.setObjectName("tabWidget")
self.tab = QtWidgets.QWidget()
self.tab.setObjectName("tab")
self.pushButton = QtWidgets.QPushButton(self.tab)
self.pushButton.setGeometry(QtCore.QRect(240, 160, 75, 23))
self.pushButton.setObjectName("pushButton")
self.tabWidget.addTab(self.tab, "")
self.tab_2 = QtWidgets.QWidget()
self.tab_2.setObjectName("tab_2")
self.tabWidget.addTab(self.tab_2, "")
self.gridLayout.addWidget(self.tabWidget, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 548, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.tabWidget.setCurrentIndex(0)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.pushButton.clicked.connect(self.openDialog)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Tab 1"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Tab 2"))
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_())
PyQt recommends not modifying the code generated by Qt Designer, instead it creates a class that inherits from the appropriate widget. On the other hand, the save and cancel buttons must be connected to the accept and reject methods, respectively. To get the information you can use exec_() that will return a value associated to the button that you have pressed, and if it is the save button then you must change the currentIndex of QTabWidget
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(392, 138)
self.gridLayout = QtWidgets.QGridLayout(Dialog)
self.gridLayout.setObjectName("gridLayout")
self.frame = QtWidgets.QFrame(Dialog)
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.frame)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.label = QtWidgets.QLabel(self.frame)
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
self.verticalLayout.addWidget(self.label)
self.lineEdit_vehicleName = QtWidgets.QLineEdit(self.frame)
self.lineEdit_vehicleName.setObjectName("lineEdit_vehicleName")
self.verticalLayout.addWidget(self.lineEdit_vehicleName)
spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem)
self.verticalLayout_2.addLayout(self.verticalLayout)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem1)
self.pushButton_save = QtWidgets.QPushButton(self.frame)
self.pushButton_save.setObjectName("pushButton_save")
self.horizontalLayout.addWidget(self.pushButton_save)
self.pushButton_cancel = QtWidgets.QPushButton(self.frame)
self.pushButton_cancel.setObjectName("pushButton_cancel")
self.horizontalLayout.addWidget(self.pushButton_cancel)
self.verticalLayout_2.addLayout(self.horizontalLayout)
self.gridLayout.addWidget(self.frame, 0, 0, 1, 1)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.label.setText(_translate("Dialog", "Enter Name:"))
self.pushButton_save.setText(_translate("Dialog", "Save"))
self.pushButton_cancel.setText(_translate("Dialog", "Cancel"))
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(548, 399)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
self.tabWidget.setObjectName("tabWidget")
self.tab = QtWidgets.QWidget()
self.tab.setObjectName("tab")
self.pushButton = QtWidgets.QPushButton(self.tab)
self.pushButton.setGeometry(QtCore.QRect(240, 160, 75, 23))
self.pushButton.setObjectName("pushButton")
self.tabWidget.addTab(self.tab, "")
self.tab_2 = QtWidgets.QWidget()
self.tab_2.setObjectName("tab_2")
self.tabWidget.addTab(self.tab_2, "")
self.gridLayout.addWidget(self.tabWidget, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 548, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.tabWidget.setCurrentIndex(0)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Tab 1"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Tab 2"))
class MainWindow(Ui_MainWindow, QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.pushButton.clicked.connect(self.openDialog)
#QtCore.pyqtSlot()
def openDialog(self):
self.dialog = Dialog()
if self.dialog.exec_() == QtWidgets.QDialog.Accepted:
self.tabWidget.setCurrentIndex(1)
class Dialog(Ui_Dialog, QtWidgets.QDialog):
def __init__(self, parent=None):
super(Dialog, self).__init__(parent)
self.setupUi(self)
self.pushButton_save.clicked.connect(self.accept)
self.pushButton_cancel.clicked.connect(self.reject)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
For more information read https://www.riverbankcomputing.com/static/Docs/PyQt5/designer.html

I have many actions in toolbar and try to make the font bold for the action which is pressed in PyQt5

I have made a UI which has various actions in three toolbars. I'm trying to make the font of one action bold when it is pressed and released by mouse. I know how to make the font bold one-by-one. But I'm going to add tens of actions in those three toolbars. How would I be able to do this? Can anyone help me?
Thank you very much.
The main function does make the font of action1 bold.
Main function:
from uitest import Ui_MainWindow
from PyQt5.QtWidgets import QMainWindow
class window(QMainWindow):
def __init__(self, parent=None):
super(window, self).__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
myFont = QtGui.QFont()
myFont.setBold(True)
self.ui.Action1.setFont(myFont)
if __name__ == "__main__":
app = QApplication([])
gui = window()
gui.show()
app.exec_()
UI code
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.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(120, 330, 151, 71))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(360, 330, 151, 71))
self.pushButton_2.setObjectName("pushButton_2")
self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit.setGeometry(QtCore.QRect(160, 80, 321, 181))
self.textEdit.setObjectName("textEdit")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.toolBar1 = QtWidgets.QToolBar(MainWindow)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.toolBar1.sizePolicy().hasHeightForWidth())
self.toolBar1.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setPointSize(14)
self.toolBar1.setFont(font)
self.toolBar1.setObjectName("toolBar1")
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.toolBar1)
self.toolBar = QtWidgets.QToolBar(MainWindow)
font = QtGui.QFont()
font.setPointSize(12)
self.toolBar.setFont(font)
self.toolBar.setObjectName("toolBar")
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.toolBar)
MainWindow.insertToolBarBreak(self.toolBar)
self.toolBar_2 = QtWidgets.QToolBar(MainWindow)
font = QtGui.QFont()
font.setPointSize(10)
self.toolBar_2.setFont(font)
self.toolBar_2.setObjectName("toolBar_2")
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.toolBar_2)
MainWindow.insertToolBarBreak(self.toolBar_2)
self.Action1 = QtWidgets.QAction(MainWindow)
self.Action1.setObjectName("Action1")
self.Action2 = QtWidgets.QAction(MainWindow)
self.Action2.setObjectName("Action2")
self.Action3 = QtWidgets.QAction(MainWindow)
self.Action3.setObjectName("Action3")
self.Action4 = QtWidgets.QAction(MainWindow)
self.Action4.setObjectName("Action4")
self.Action5 = QtWidgets.QAction(MainWindow)
self.Action5.setObjectName("Action5")
self.Action6 = QtWidgets.QAction(MainWindow)
self.Action6.setObjectName("Action6")
self.Action7 = QtWidgets.QAction(MainWindow)
self.Action7.setObjectName("Action7")
self.Action8 = QtWidgets.QAction(MainWindow)
self.Action8.setObjectName("Action8")
self.Action9 = QtWidgets.QAction(MainWindow)
self.Action9.setObjectName("Action9")
self.toolBar1.addAction(self.Action1)
self.toolBar1.addSeparator()
self.toolBar1.addAction(self.Action2)
self.toolBar1.addSeparator()
self.toolBar1.addAction(self.Action3)
self.toolBar.addAction(self.Action4)
self.toolBar.addSeparator()
self.toolBar.addAction(self.Action5)
self.toolBar.addSeparator()
self.toolBar.addAction(self.Action6)
self.toolBar_2.addAction(self.Action7)
self.toolBar_2.addSeparator()
self.toolBar_2.addAction(self.Action8)
self.toolBar_2.addSeparator()
self.toolBar_2.addAction(self.Action9)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
self.pushButton_2.setText(_translate("MainWindow", "PushButton"))
self.toolBar1.setWindowTitle(_translate("MainWindow", "toolBar"))
self.toolBar.setWindowTitle(_translate("MainWindow", "toolBar"))
self.toolBar_2.setWindowTitle(_translate("MainWindow", "toolBar_2"))
self.Action1.setText(_translate("MainWindow", "Action1"))
self.Action2.setText(_translate("MainWindow", "Action2"))
self.Action3.setText(_translate("MainWindow", "Action3"))
self.Action4.setText(_translate("MainWindow", "Action4"))
self.Action5.setText(_translate("MainWindow", "Action5"))
self.Action6.setText(_translate("MainWindow", "Action6"))
self.Action7.setText(_translate("MainWindow", "Action7"))
self.Action8.setText(_translate("MainWindow", "Action8"))
self.Action9.setText(_translate("MainWindow", "Action9"))
UI is created by PyQt5 designer.
One way to do this would be to connect to the actionTriggered signal of each toolbar, which sends the action that was clicked. Assuming you only want one action bold at a time, you can then use this to turn on bold for the current action and turn off bold for the previous action (if there is one):
class window(QMainWindow):
def __init__(self, parent=None):
...
for toolbar in self.findChildren(QtWidgets.QToolBar):
toolbar.actionTriggered.connect(self.setBoldAction)
def setBoldAction(self, target, toolbar=None):
if toolbar is None:
toolbar = self.sender()
for action in toolbar.actions():
if not action.isSeparator():
font = action.font()
font.setBold(action is target)
action.setFont(font)
If you want to set the bold action programmatically, you can also do:
self.setBoldAction(action, toolbar)

ui_mainwindow' object has no attribute 'connect'

I am new to PyQt. I want the GUI to be GUI1 from time 0 to time t1, GUI2 from time t1 to t2, ..., GUIn from time t(n-1) to t(n). So I thought I would have to use the multithreading techniques. Does anyone have available code that has the same function as my requirement?
Moreover, I met some problem when I try connect the thread's finished signal to the UpdateUI function. The code is shown below:
class Ui_MainWindow(QtCore.QObject):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(771, 586)
MainWindow.setMinimumSize(QtCore.QSize(771, 586))
font = QtGui.QFont()
font.setFamily(_fromUtf8("Arial"))
MainWindow.setFont(font)
#Feng
self.centralWidget = QtGui.QWidget(MainWindow)
self.centralWidget.setObjectName(_fromUtf8("centralWidget"))
self.layoutWidget = QtGui.QWidget(self.centralWidget)
self.layoutWidget.setGeometry(QtCore.QRect(10, 500, 751, 27))
self.layoutWidget.setObjectName(_fromUtf8("layoutWidget"))
self.horizontalLayout = QtGui.QHBoxLayout(self.layoutWidget)
self.horizontalLayout.setSizeConstraint(QtGui.QLayout.SetMinimumSize)
self.horizontalLayout.setMargin(11)
self.horizontalLayout.setSpacing(6)
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.btnSkip = QtGui.QPushButton(self.layoutWidget)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.btnSkip.sizePolicy().hasHeightForWidth())
self.btnSkip.setSizePolicy(sizePolicy)
self.btnSkip.setMinimumSize(QtCore.QSize(0, 23))
self.btnSkip.setFlat(False)
self.btnSkip.setObjectName(_fromUtf8("btnSkip"))
self.horizontalLayout.addWidget(self.btnSkip)
self.horizontalSlider = QtGui.QSlider(self.layoutWidget)
self.horizontalSlider.setMinimumSize(QtCore.QSize(0, 22))
self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
self.horizontalSlider.setObjectName(_fromUtf8("horizontalSlider"))
self.horizontalLayout.addWidget(self.horizontalSlider)
self.btnPrevious = QtGui.QPushButton(self.layoutWidget)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.btnPrevious.sizePolicy().hasHeightForWidth())
self.btnPrevious.setSizePolicy(sizePolicy)
self.btnPrevious.setMinimumSize(QtCore.QSize(0, 23))
self.btnPrevious.setObjectName(_fromUtf8("btnPrevious"))
self.horizontalLayout.addWidget(self.btnPrevious)
self.btnNext = QtGui.QPushButton(self.layoutWidget)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.btnNext.sizePolicy().hasHeightForWidth())
self.btnNext.setSizePolicy(sizePolicy)
self.btnNext.setMinimumSize(QtCore.QSize(0, 23))
self.btnNext.setObjectName(_fromUtf8("btnNext"))
self.horizontalLayout.addWidget(self.btnNext)
self.layoutWidget1 = QtGui.QWidget(self.centralWidget)
self.layoutWidget1.setGeometry(QtCore.QRect(11, 11, 751, 171))
self.layoutWidget1.setObjectName(_fromUtf8("layoutWidget1"))
self.verticalLayout = QtGui.QVBoxLayout(self.layoutWidget1)
self.verticalLayout.setMargin(11)
self.verticalLayout.setSpacing(6)
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.textEditTaskName = QtGui.QTextEdit(self.layoutWidget1)
self.textEditTaskName.setEnabled(False)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.textEditTaskName.sizePolicy().hasHeightForWidth())
self.textEditTaskName.setSizePolicy(sizePolicy)
self.textEditTaskName.setMinimumSize(QtCore.QSize(0, 94))
font = QtGui.QFont()
font.setFamily(_fromUtf8("Arial"))
font.setPointSize(18)
font.setBold(True)
font.setWeight(75)
self.textEditTaskName.setFont(font)
self.textEditTaskName.setAcceptDrops(False)
self.textEditTaskName.setFrameShape(QtGui.QFrame.NoFrame)
#Feng
self.textEditTaskName.setText("Welcome")
self.textEditTaskName.setObjectName(_fromUtf8("textEditTaskName"))
self.verticalLayout.addWidget(self.textEditTaskName)
self.textEditTaskdesciption = QtGui.QTextEdit(self.layoutWidget1)
self.textEditTaskdesciption.setEnabled(False)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(1)
sizePolicy.setHeightForWidth(self.textEditTaskdesciption.sizePolicy().hasHeightForWidth())
self.textEditTaskdesciption.setSizePolicy(sizePolicy)
self.textEditTaskdesciption.setMinimumSize(QtCore.QSize(0, 69))
font = QtGui.QFont()
font.setFamily(_fromUtf8("Arial"))
font.setPointSize(12)
self.textEditTaskdesciption.setFont(font)
self.textEditTaskdesciption.setAcceptDrops(False)
#Feng
self.textEditTaskdesciption.setText('Welcome to my program.')
self.textEditTaskdesciption.setFrameShape(QtGui.QFrame.NoFrame)
self.textEditTaskdesciption.setFrameShadow(QtGui.QFrame.Plain)
self.textEditTaskdesciption.setObjectName(_fromUtf8("textEditTaskdesciption"))
self.verticalLayout.addWidget(self.textEditTaskdesciption)
self.layoutWidget2 = QtGui.QWidget(self.centralWidget)
self.layoutWidget2.setGeometry(QtCore.QRect(10, 190, 751, 311))
self.layoutWidget2.setObjectName(_fromUtf8("layoutWidget2"))
self.verticalLayout_2 = QtGui.QVBoxLayout(self.layoutWidget2)
self.verticalLayout_2.setMargin(11)
self.verticalLayout_2.setSpacing(6)
self.verticalLayout_2.setObjectName(_fromUtf8("verticalLayout_2"))
self.labelTESTImage = QtGui.QLabel(self.layoutWidget2)
font = QtGui.QFont()
font.setFamily(_fromUtf8("Arial"))
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.labelTESTImage.setFont(font)
self.labelTESTImage.setAlignment(QtCore.Qt.AlignCenter)
image = QtGui.QImage('Images/deptLogo.png')
#Feng
self.labelTESTImage.setPixmap(QPixmap.fromImage(image))#("deptLogo.png"))
self.labelTESTImage.setScaledContents(True)
self.labelTESTImage.setObjectName(_fromUtf8("labelTESTImage"))
self.labelTESTImage.setScaledContents(True)
#Feng
self.btnNext.pressed.connect(self.nextSession)
self.btnPrevious.pressed.connect(self.previousSession)
#Feng
self.horizontalSlider.setRange(-1,len(strSettings)-1)
self.horizontalSlider.setValue(-1)
self.horizontalSlider.valueChanged.connect(self.dragSlider)
#self.btnNext.clicked.connect(self.readingSettingsFile)
#self.btnNext.clicked.connect(self.loadimage)
self.verticalLayout_2.addWidget(self.labelTESTImage)
self.progressBar = QtGui.QProgressBar(self.layoutWidget2)
self.progressBar.setMinimumSize(QtCore.QSize(0, 22))
self.progressBar.setProperty("value", 24)
self.progressBar.setObjectName(_fromUtf8("progressBar"))
self.progressBar.setVisible(False)
self.verticalLayout_2.addWidget(self.progressBar)
MainWindow.setCentralWidget(self.centralWidget)
self.menuBar = QtGui.QMenuBar(MainWindow)
self.menuBar.setGeometry(QtCore.QRect(0, 0, 771, 21))
self.menuBar.setObjectName(_fromUtf8("menuBar"))
self.menuFile = QtGui.QMenu(self.menuBar)
self.menuFile.setObjectName(_fromUtf8("menuFile"))
self.menuEdit = QtGui.QMenu(self.menuBar)
self.menuEdit.setObjectName(_fromUtf8("menuEdit"))
self.menuHelp = QtGui.QMenu(self.menuBar)
self.menuHelp.setObjectName(_fromUtf8("menuHelp"))
MainWindow.setMenuBar(self.menuBar)
self.mainToolBar = QtGui.QToolBar(MainWindow)
self.mainToolBar.setObjectName(_fromUtf8("mainToolBar"))
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
self.statusBar = QtGui.QStatusBar(MainWindow)
self.statusBar.setObjectName(_fromUtf8("statusBar"))
MainWindow.setStatusBar(self.statusBar)
self.menuBar.addAction(self.menuFile.menuAction())
self.menuBar.addAction(self.menuEdit.menuAction())
self.menuBar.addAction(self.menuHelp.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "my test", None))
MainWindow.setWindowIcon(QtGui.QIcon('Images/test.ico'))
self.btnSkip.setText(_translate("MainWindow", "Skip", None))
self.btnPrevious.setText(_translate("MainWindow", "&Previous", None))
self.btnNext.setText(_translate("MainWindow", "&Next", None))
#self.labelTESTImage.setText(_translate("MainWindow", "my test", None))
self.menuFile.setTitle(_translate("MainWindow", "File", None))
self.menuEdit.setTitle(_translate("MainWindow", "Edit", None))
self.menuHelp.setTitle(_translate("MainWindow", "Help", None))
#Feng
def dragSlider(self):
global strSettings, timeSleep, lineCurrent
lineCurrent = int(self.horizontalSlider.value())
if (lineCurrent != -1):
line = strSettings[lineCurrent]
wavFilename, showProgressBar, valPauseTime, valName, valDescription, valImageLoad = line.split("\t")
self.time_thread = timerThread(float(valPauseTime))
self.connect(self.time_thread, SIGNAL("finished()"),self, self.updateUIFromFile())
else:
self.btnNext.setText(_translate("MainWindow", "&Start", None))
self.textEditTaskName.setText("Welcome")
self.textEditTaskdesciption.setText('This program is designed by Feng.')
self.labelOTCImage.setPixmap(QPixmap.fromImage(QtGui.QImage('Images/deptLogo.png')))
self.labelOTCImage.setScaledContents(True)
self.progressBar.setVisible(False)
def nextSession(self):
global strSettings, timeSleep, lineCurrent
if(lineCurrent<len(strSettings)-1):
lineCurrent += 1
self.horizontalSlider.setValue(lineCurrent)
#self.updateUIFromFile()
def previousSession(self):
global strSettings, timeSleep, lineCurrent
if lineCurrent>0:
lineCurrent -= 1
else:
lineCurrent = -1
self.horizontalSlider.setValue(lineCurrent)
#self.updateUIFromFile()
def updateUIFromFile(self):
self.btnNext.setText(_translate("MainWindow", "&Next", None))
line = strSettings[lineCurrent]
wavFilename, showProgressBar, valPauseTime, valName, valDescription, valImageLoad = line.split("\t")
self.textEditTaskName.setText(valName)
self.textEditTaskdesciption.setText(valDescription)
self.labelOTCImage.setPixmap(QPixmap.fromImage(QtGui.QImage('resources/'+valImageLoad.replace("\n", ""))))
self.labelOTCImage.setScaledContents(False)
self.progressBar.setVisible(int(showProgressBar))
self.progressBar.setValue(50)
This is the thread class
class timerThread(QtCore.QThread):
def __init__(self,sleepTime):
QtCore.QThread.__init__(self)
self.sleepTime = sleepTime
def __del__(self):
self.wait()
def run(self):
time.sleep(self.sleepTime) # artificial time delay
Main
if __name__ == "__main__":
try:
fileSettings = open('protocol.txt','r')
except IOError as e:
print ("Unable to open file") #Does not exist OR no read permissions
sys.exit(0)
strSettings = fileSettings.readlines()
strSettings = list(filter(None, strSettings))
fileSettings.close()
timeSleep = 0
lineCurrent = -1
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
The file 'protocol.txt' contains the contents for each QtWidget and a timeline for when the GUI would change contents
You're using the Ui_MainWindow class incorrectly. See this answer for how to use the auto-generated UI classes.
You should be either inheriting from the UI class, or setting it as an instance attribute on one of your own classes.
class MainWIndow(QtGui.QMainWIndow,Ui_MainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.setupUi(self)
Sometimes, people won't inherit, but will assign it to an attribute of the class:
class MainWIndow(QtGui.QMainWIndow):
def __init__(self, parent=None):
QtGui.QMainWIndow.__init__(self, parent)
self.ui = Ui_MainWindow(self)
self.ui.setupUi(self)
So although nobody wants to give the answer to this question, I think I found the answer from another question asked by me in Update GUI at different time interval in QT.
I hope someone could give me the pyqt version of this answer because this is in C++.

Getting a blank form when running PyQt application

I wrote a small PyQt program based on an example from a book. I edited the code to add some niceties. Below is my code:
from __future__ import division
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
class Ui_MainWindow(QWidget):
def __init__(self):
QWidget.__init__(self)
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(9, 9, 781, 461))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setObjectName("verticalLayout")
self.textBrowser = QtWidgets.QTextBrowser(self.verticalLayoutWidget)
self.textBrowser.setObjectName("textBrowser")
self.verticalLayout.addWidget(self.textBrowser)
self.horizontalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.horizontalLayoutWidget.setGeometry(QtCore.QRect(10, 480, 781, 80))
self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label = QtWidgets.QLabel(self.horizontalLayoutWidget)
font = QtGui.QFont()
font.setPointSize(16)
self.label.setFont(font)
self.label.setObjectName("label")
self.horizontalLayout_2.addWidget(self.label)
self.lineEdit = QtWidgets.QLineEdit(self.horizontalLayoutWidget)
self.lineEdit.setBaseSize(QtCore.QSize(0, 0))
font = QtGui.QFont()
font.setPointSize(12)
self.lineEdit.setFont(font)
self.lineEdit.setText("")
self.lineEdit.setObjectName("lineEdit")
self.horizontalLayout_2.addWidget(self.lineEdit)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
self.menuFile = QtWidgets.QMenu(self.menubar)
self.menuFile.setObjectName("menuFile")
MainWindow.setMenuBar(self.menubar)
self.actionQuit = QtWidgets.QAction(MainWindow)
self.actionQuit.setObjectName("actionQuit")
self.menuFile.addAction(self.actionQuit)
self.menubar.addAction(self.menuFile.menuAction())
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", "Expression:"))
self.menuFile.setTitle(_translate("MainWindow", "File"))
self.actionQuit.setText(_translate("MainWindow", "Quit (Ctrl+Q)"))
self.actionQuit.setShortcut(_translate("MainWindow", "Ctrl+Q"))
self.lineEdit.setFocus()
self.connect(self.lineEdit, self.updateUi)
self.setWindowTitle("Calculate")
def updateUi(self):
try:
text = unicode(self.lineEdit.text())
self.browser.append("%s = <b>%s</b>" % (text,eval(text)))
except:
self.browser.append("<font color=red>Invalid Expression</font>")
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Ui_MainWindow()
window.show()
sys.exit(app.exec_())
Output:
But when I execute this file I only get a blank window without any widgets inside it. What is the error in my code?
You defined setupUi() function but I don't see you executing it anywhere. You should expand your __init__ with:
class Ui_MainWindow(QWidget):
def __init__(self):
QWidget.__init__(self)
self.setupUi(your_main_window)

Categories

Resources