Change main window active tab from a dialogbox action - python

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

Related

How to assign a value to a variable from a splitbox?

I created an elementary template in Qt designer with two windows (Qspinbox) and one button (QPushButton). The idea is this. The user enters numbers in both fields, presses the button and the program performs certain actions with these numbers. (For example, it adds up). I understand that you need to create a function that will specify what needs to be done with the numbers entered by the user. Bind the "button click" event to this function. (I can link a button click to a function.) But here's how I assign the values entered by the user to the variables for further work with them (with variables). PLEASE help, I'm fighting for the second week! Here is the source code:
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(507, 294)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(270, 200, 75, 23))
self.pushButton.setObjectName("pushButton")
self.layoutWidget = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget.setGeometry(QtCore.QRect(90, 100, 291, 22))
self.layoutWidget.setObjectName("layoutWidget")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.layoutWidget)
self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label_2 = QtWidgets.QLabel(self.layoutWidget)
self.label_2.setObjectName("label_2")
self.horizontalLayout_2.addWidget(self.label_2)
self.spinBox_2 = QtWidgets.QSpinBox(self.layoutWidget)
self.spinBox_2.setObjectName("spinBox_2")
self.horizontalLayout_2.addWidget(self.spinBox_2)
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(90, 30, 291, 22))
self.widget.setObjectName("widget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.widget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(self.widget)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.spinBox = QtWidgets.QSpinBox(self.widget)
self.spinBox.setObjectName("spinBox")
self.horizontalLayout.addWidget(self.spinBox)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 507, 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"))
self.pushButton.setText(_translate("MainWindow", "calculate"))
self.label_2.setText(_translate("MainWindow", "Input second number:"))
self.label.setText(_translate("MainWindow", "Input first number:"))
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_())
And here's what I added to implement the goal. What am I doing wrong?
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(507, 294)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(270, 200, 75, 23))
self.pushButton.setObjectName("pushButton")
self.layoutWidget = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget.setGeometry(QtCore.QRect(90, 100, 291, 22))
self.layoutWidget.setObjectName("layoutWidget")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.layoutWidget)
self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label_2 = QtWidgets.QLabel(self.layoutWidget)
self.label_2.setObjectName("label_2")
self.horizontalLayout_2.addWidget(self.label_2)
self.spinBox_2 = QtWidgets.QSpinBox(self.layoutWidget)
self.spinBox_2.setObjectName("spinBox_2")
self.horizontalLayout_2.addWidget(self.spinBox_2)
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(90, 30, 291, 22))
self.widget.setObjectName("widget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.widget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(self.widget)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.spinBox = QtWidgets.QSpinBox(self.widget)
self.spinBox.setObjectName("spinBox")
self.horizontalLayout.addWidget(self.spinBox)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 507, 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"))
self.pushButton.setText(_translate("MainWindow", "calculate"))
self.label_2.setText(_translate("MainWindow", "Input second number:"))
self.label.setText(_translate("MainWindow", "Input first number:"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
def calc(self):
first_n = self.label_2.value()
second_n = self.label.value()
result = first_n + second_n
print(result)
ui.pushButton.clicked.connect(calc)
sys.exit(app.exec_())
I believe that the calc method would be better written above "if __name__ == "__main__":"
5:41
but then there is a trace error: NameError: name 'calc' is not defined
In Python, the name self is used to mention the current object. In your code, you should define calc inside the class Ui_mainwindow, which makes calc a method of Ui_mainwindow, instead of outside the Class.

How do I show a subwindow in a MDI area?

So am testing a bit with MDI area, the problem is that when I try to Open a Subwindow inside the MDI this happens.
Any way to make the Little Subwindow bigger enough to be readble?
Am doing Everything inside an only test file.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(488, 392)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_3.setGeometry(QtCore.QRect(280, 290, 91, 51))
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_3.clicked.connect(self.Open)
self.mdiArea = QtWidgets.QMdiArea(self.centralwidget)
self.mdiArea.setGeometry(QtCore.QRect(60, 20, 261, 211))
self.mdiArea.setObjectName("mdiArea")
self.subwindow = QtWidgets.QWidget()
self.subwindow.setObjectName("subwindow")
self.pushButton = QtWidgets.QPushButton(self.subwindow)
self.pushButton.setGeometry(QtCore.QRect(100, 80, 56, 17))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 488, 18))
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 Open(self):
self.mdiArea.addSubWindow(self.subwindow)
self.subwindow.show()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton_3.setText(_translate("MainWindow", "Open"))
self.subwindow.setWindowTitle(_translate("MainWindow", "Subwindow"))
self.pushButton.setText(_translate("MainWindow", "YAY!"))
class mywindow(QtWidgets.QMainWindow):
def __init__(self):
super(mywindow, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
app = QtWidgets.QApplication([])
application = mywindow()
application.show()
app.exec()
You have to resize the QMdiSubWindow:
def Open(self):
sw = self.mdiArea.addSubWindow(self.subwindow)
self.subwindow.show()
sw.resize(640, 480)

"Command terminated" when using PyQt5 QtWidgets QGraphicsScene()

I am trying to open an image with PyQt5 QtWidgets QGraphicsView and using QGraphicsScene from the same module. When I am calling QtWidgets.QGraphicsScene.addPixmap(pix) the app closes with the output Command terminated. Below is the code:
import os, sys
from PyQt5 import QtGui, QtCore, QtWidgets
sys.path.insert(0,os.getcwd()+'/'+'gui')
sys.path.insert(0,os.getcwd()+'/'+'src')
from Widget import Ui_MainWindow
from PIL import Image, ImageQt
class Window(QtWidgets.QMainWindow,Ui_MainWindow):
def __init__(self):
super(Window,self).__init__()
self.setupUi(self)
self.actionOpen.setShortcut("Ctrl+O")
self.actionOpen.setStatusTip("open an image")
self.actionOpen.triggered.connect(self.file_open)
self.show()
def file_open(self):
filepath,_ = QtWidgets.QFileDialog.getOpenFileName(self,'Open File')
if filepath:
pim = Image.open(str(filepath))
qim = ImageQt.ImageQt(pim)
pix = QtGui.QPixmap.fromImage(qim)
self.scene = QtWidgets.QGraphicsScene()
self.scene.addPixmap(pix)
self.view = self.graphicsView.setScene(self.scene)
else:
pass
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = Window()
sys.exit(app.exec_())
Additional Info: I used Qt Designer to create Widget.ui file and converted to Widget.py. My machine host is windows and I am running Ubuntu 18.04 in virtualbox. My python version is 2.7. I would really appreciate any help (I spent more than a day on this problem) and can provide any missing info. Thanks
and below is my ui converted to py:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Widget.ui'
#
# Created by: PyQt5 UI code generator 5.10.1
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1115, 689)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.horizontalLayoutWidget_2 = QtWidgets.QWidget(self.centralwidget)
self.horizontalLayoutWidget_2.setGeometry(QtCore.QRect(6, 10, 1091, 601))
self.horizontalLayoutWidget_2.setObjectName("horizontalLayoutWidget_2")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget_2)
self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.treeView = QtWidgets.QTreeView(self.horizontalLayoutWidget_2)
self.treeView.setObjectName("treeView")
self.verticalLayout.addWidget(self.treeView)
self.verticalLayout_3 = QtWidgets.QVBoxLayout()
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.gridLayout_2 = QtWidgets.QGridLayout()
self.gridLayout_2.setObjectName("gridLayout_2")
self.pushButton_4 = QtWidgets.QPushButton(self.horizontalLayoutWidget_2)
self.pushButton_4.setMinimumSize(QtCore.QSize(90, 20))
self.pushButton_4.setObjectName("pushButton_4")
self.gridLayout_2.addWidget(self.pushButton_4, 0, 1, 1, 1)
self.pushButton_2 = QtWidgets.QPushButton(self.horizontalLayoutWidget_2)
self.pushButton_2.setMinimumSize(QtCore.QSize(90, 20))
self.pushButton_2.setObjectName("pushButton_2")
self.gridLayout_2.addWidget(self.pushButton_2, 1, 0, 1, 1)
self.pushButton_5 = QtWidgets.QPushButton(self.horizontalLayoutWidget_2)
self.pushButton_5.setMinimumSize(QtCore.QSize(90, 20))
self.pushButton_5.setObjectName("pushButton_5")
self.gridLayout_2.addWidget(self.pushButton_5, 1, 1, 1, 1)
self.pushButton = QtWidgets.QPushButton(self.horizontalLayoutWidget_2)
self.pushButton.setMinimumSize(QtCore.QSize(90, 20))
self.pushButton.setObjectName("pushButton")
self.gridLayout_2.addWidget(self.pushButton, 0, 0, 1, 1)
self.pushButton_3 = QtWidgets.QPushButton(self.horizontalLayoutWidget_2)
self.pushButton_3.setMinimumSize(QtCore.QSize(90, 20))
self.pushButton_3.setObjectName("pushButton_3")
self.gridLayout_2.addWidget(self.pushButton_3, 0, 2, 1, 1)
self.pushButton_6 = QtWidgets.QPushButton(self.horizontalLayoutWidget_2)
self.pushButton_6.setMinimumSize(QtCore.QSize(90, 20))
self.pushButton_6.setObjectName("pushButton_6")
self.gridLayout_2.addWidget(self.pushButton_6, 1, 2, 1, 1)
self.verticalLayout_3.addLayout(self.gridLayout_2)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem)
self.label = QtWidgets.QLabel(self.horizontalLayoutWidget_2)
self.label.setLayoutDirection(QtCore.Qt.LeftToRight)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.rotation = QtWidgets.QDoubleSpinBox(self.horizontalLayoutWidget_2)
self.rotation.setMinimumSize(QtCore.QSize(100, 20))
self.rotation.setObjectName("rotation")
self.horizontalLayout.addWidget(self.rotation)
self.verticalLayout_3.addLayout(self.horizontalLayout)
self.verticalLayout.addLayout(self.verticalLayout_3)
self.logRun = QtWidgets.QPlainTextEdit(self.horizontalLayoutWidget_2)
self.logRun.setObjectName("logRun")
self.verticalLayout.addWidget(self.logRun)
self.horizontalLayout_2.addLayout(self.verticalLayout)
self.graphicsView = QtWidgets.QGraphicsView(self.horizontalLayoutWidget_2)
self.graphicsView.setMinimumSize(QtCore.QSize(500, 0))
self.graphicsView.setObjectName("graphicsView")
self.horizontalLayout_2.addWidget(self.graphicsView)
self.verticalLayout_2 = QtWidgets.QVBoxLayout()
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.tabWidget = QtWidgets.QTabWidget(self.horizontalLayoutWidget_2)
self.tabWidget.setObjectName("tabWidget")
self.tab = QtWidgets.QWidget()
self.tab.setObjectName("tab")
self.tabWidget.addTab(self.tab, "")
self.tab_2 = QtWidgets.QWidget()
self.tab_2.setObjectName("tab_2")
self.tabWidget.addTab(self.tab_2, "")
self.verticalLayout_2.addWidget(self.tabWidget)
self.extractedText = QtWidgets.QPlainTextEdit(self.horizontalLayoutWidget_2)
self.extractedText.setObjectName("extractedText")
self.verticalLayout_2.addWidget(self.extractedText)
self.horizontalLayout_2.addLayout(self.verticalLayout_2)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1115, 21))
self.menubar.setObjectName("menubar")
self.menuFile = QtWidgets.QMenu(self.menubar)
self.menuFile.setObjectName("menuFile")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionOpen = QtWidgets.QAction(MainWindow)
self.actionOpen.setObjectName("actionOpen")
self.actionCtrl_S = QtWidgets.QAction(MainWindow)
self.actionCtrl_S.setObjectName("actionCtrl_S")
self.actionSave_3 = QtWidgets.QAction(MainWindow)
self.actionSave_3.setObjectName("actionSave_3")
self.actionExit = QtWidgets.QAction(MainWindow)
self.actionExit.setObjectName("actionExit")
self.menuFile.addAction(self.actionOpen)
self.menuFile.addAction(self.actionSave_3)
self.menuFile.addSeparator()
self.menuFile.addAction(self.actionExit)
self.menubar.addAction(self.menuFile.menuAction())
self.retranslateUi(MainWindow)
self.tabWidget.setCurrentIndex(0)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
MainWindow.setTabOrder(self.logRun, self.extractedText)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton_4.setText(_translate("MainWindow", "PushButton"))
self.pushButton_2.setText(_translate("MainWindow", "remove black"))
self.pushButton_5.setText(_translate("MainWindow", "PushButton"))
self.pushButton.setText(_translate("MainWindow", "whiten"))
self.pushButton_3.setText(_translate("MainWindow", "PushButton"))
self.pushButton_6.setText(_translate("MainWindow", "PushButton"))
self.label.setText(_translate("MainWindow", "Rotation"))
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"))
self.menuFile.setTitle(_translate("MainWindow", "File"))
self.actionOpen.setText(_translate("MainWindow", "Open"))
self.actionCtrl_S.setText(_translate("MainWindow", "Ctrl+S"))
self.actionSave_3.setText(_translate("MainWindow", "Save"))
self.actionExit.setText(_translate("MainWindow", "Exit"))
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 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)

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