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)
Related
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)
I'm building restaurant app for practice with PyQt5 i have 2 problems:
I have a main screen that will take the user to other screens
when changing between screens in the second time i get error and it crash
The previous window won't close when i'm moving to the new window so every time it's new window
I tried searching for information about it and all i can find is how to do things in the same screen not in a new screen
Code for you to see yourself:
first screen:
from mainMenu import*
from PyQt5 import QtCore, QtGui, QtWidgets
mod=0#0 - TA , 1- Delivery
class Ui_Main():
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1072, 747)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.ta = QtWidgets.QPushButton(self.centralwidget)
self.ta.setGeometry(QtCore.QRect(680, 50, 191, 171))
self.ta.setObjectName("ta")
self.ta.clicked.connect(self.taClicked)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1072, 22))
self.menubar.setObjectName("menubar")
self.menuMain = QtWidgets.QMenu(self.menubar)
self.menuMain.setObjectName("menuMain")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.menubar.addAction(self.menuMain.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow",
"MainWindow"))
self.ta.setText(_translate("MainWindow", "TA"))
self.menuMain.setTitle(_translate("MainWindow", "Main"))
def taClicked(self):
mod=0
print("here")
self.window = QtWidgets.QMainWindow()
self.ui = Ui_MainMenu() # ------------->creating an object
self.ui.setupUi(self.window)
self.window.show()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_Main()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Second screen:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from main import*
class Ui_MainMenu(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1073, 738)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.graphicsView = QtWidgets.QGraphicsView(self.centralwidget)
self.graphicsView.setGeometry(QtCore.QRect(685, 40, 331, 251))
self.graphicsView.setObjectName("graphicsView")
self.line = QtWidgets.QFrame(self.centralwidget)
self.line.setGeometry(QtCore.QRect(80, 330, 561, 20))
self.line.setFrameShape(QtWidgets.QFrame.HLine)
self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line.setObjectName("line")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(190, 385, 71, 41))
self.label.setObjectName("label")
self.pushButton_14 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_14.setGeometry(QtCore.QRect(190, 440, 81, 51))
self.pushButton_14.setObjectName("pushButton_14")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(800, 380, 81, 16))
self.label_2.setObjectName("label_2")
self.spinBox = QtWidgets.QSpinBox(self.centralwidget)
self.spinBox.setGeometry(QtCore.QRect(727, 370, 61, 41))
self.spinBox.setObjectName("spinBox")
self.pushButton_40 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_40.setGeometry(QtCore.QRect(750, 600, 141, 41))
self.pushButton_40.setObjectName("pushButton_40")
self.pushButton_40.clicked.connect(self.btnclicked)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1073, 22))
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("MainMenu", "MainMenu"))
self.pushButton_40.setText(_translate("MainWindow", "Cancel"))
def btnclicked(self):
print("here")
self.window = QtWidgets.QMainWindow()
self.m= Ui_Main()
self.m.setupUi(self.window)
self.window.show()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainMenu()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Error:
Traceback (most recent call last):
File "/Users/adir/PycharmProjects/main.py", line 61, in taClicked
self.ui = Ui_MainMenu() # ------------->creating an object
NameError: name 'Ui_MainMenu' is not defined
Try it:
#from mainMenu import*
from PyQt5 import QtCore, QtGui, QtWidgets
mod = 0 #0 - TA , 1- Delivery
class Ui_MainMenu(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1073, 738)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.graphicsView = QtWidgets.QGraphicsView(self.centralwidget)
self.graphicsView.setGeometry(QtCore.QRect(685, 40, 331, 251))
self.graphicsView.setObjectName("graphicsView")
self.line = QtWidgets.QFrame(self.centralwidget)
self.line.setGeometry(QtCore.QRect(80, 330, 561, 20))
self.line.setFrameShape(QtWidgets.QFrame.HLine)
self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line.setObjectName("line")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(190, 385, 71, 41))
self.label.setObjectName("label")
self.pushButton_14 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_14.setGeometry(QtCore.QRect(190, 440, 81, 51))
self.pushButton_14.setObjectName("pushButton_14")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(800, 380, 81, 16))
self.label_2.setObjectName("label_2")
self.spinBox = QtWidgets.QSpinBox(self.centralwidget)
self.spinBox.setGeometry(QtCore.QRect(727, 370, 61, 41))
self.spinBox.setObjectName("spinBox")
self.pushButton_40 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_40.setGeometry(QtCore.QRect(750, 600, 141, 41))
self.pushButton_40.setObjectName("pushButton_40")
# self.pushButton_40.clicked.connect(self.btnclicked)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1073, 22))
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("MainMenu", "MainMenu"))
self.pushButton_40.setText(_translate("MainWindow", "Cancel"))
class Ui_Main():
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1072, 747)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.ta = QtWidgets.QPushButton(self.centralwidget)
self.ta.setGeometry(QtCore.QRect(680, 50, 191, 171))
self.ta.setObjectName("ta")
# self.ta.clicked.connect(self.taClicked)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1072, 22))
self.menubar.setObjectName("menubar")
self.menuMain = QtWidgets.QMenu(self.menubar)
self.menuMain.setObjectName("menuMain")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.menubar.addAction(self.menuMain.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow",
"MainWindow"))
self.ta.setText(_translate("MainWindow", "TA"))
self.menuMain.setTitle(_translate("MainWindow", "Main"))
class MainWindow(QtWidgets.QMainWindow, Ui_Main):
def __init__(self):
super().__init__()
self.setupUi(self)
self.ta.clicked.connect(self.taClicked)
def taClicked(self):
mod = 0
print("here")
self.window = QtWidgets.QMainWindow()
self.mainMenu = Ui_MainMenu() # ------------->creating an object
self.mainMenu.setupUi(self.window)
self.mainMenu.pushButton_40.clicked.connect(self.btnclicked)
self.window.show()
self.hide()
def btnclicked(self):
self.window.hide()
self.show()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
# MainWindow = QtWidgets.QMainWindow()
# ui = Ui_Main()
# ui.setupUi(MainWindow)
# MainWindow.show()
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())
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
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(9576, 698)
font = QtGui.QFont()
font.setFamily("Agency FB")
font.setPointSize(9)
MainWindow.setFont(font)
MainWindow.setFocusPolicy(QtCore.Qt.StrongFocus)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(20, 30, 221, 421))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.RButton = QtWidgets.QRadioButton(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setPointSize(20)
self.RButton.setFont(font)
self.RButton.setObjectName("RButton")
self.verticalLayout.addWidget(self.RButton)
self.RButton2 = QtWidgets.QRadioButton(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setPointSize(20)
self.RButton2.setFont(font)
self.RButton2.setObjectName("RButton2")
self.verticalLayout.addWidget(self.RButton2)
self.RButton3 = QtWidgets.QRadioButton(self.verticalLayoutWidget)
font = QtGui.QFont()
font.setPointSize(20)
self.RButton3.setFont(font)
self.RButton3.setObjectName("RButton3")
self.verticalLayout.addWidget(self.RButton3)
self.Button = QtWidgets.QPushButton(self.centralwidget)
self.Button.setGeometry(QtCore.QRect(390, 150, 351, 221))
font = QtGui.QFont()
font.setFamily("Agency FB")
font.setPointSize(24)
self.Button.setFont(font)
self.Button.setFocusPolicy(QtCore.Qt.NoFocus)
self.Button.setAutoDefault(False)
self.Button.setObjectName("Button")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 957, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.Button.clicked.connect(self.btn_clicked)
self.RButton.clicked.connect(self.btn_clicked)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def btn_clicked(self):
QMessageBox.about(self,"창","안녕")
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.RButton.setText(_translate("MainWindow", "버튼1"))
self.RButton2.setText(_translate("MainWindow", "버튼2"))
self.RButton3.setText(_translate("MainWindow", "버튼3"))
self.Button.setText(_translate("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_())
when button clicked program is turn off...
I need help
The problem is caused because QMessageBox.about() requires as a first parameter a widget but self, that is Ui_MainWindow, is not a widget. Qt Designer does not generate a widget but creates a class that fills a widget. It is also not recommended to modify the class generated by Qt Designer, it is appropriate to create a class that inherits from the appropriate widget and use the design class
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, *args, **kwargs):
QtWidgets.QMainWindow.__init__(self, *args, **kwargs)
self.setupUi(self)
self.Button.clicked.connect(self.btn_clicked)
self.RButton.clicked.connect(self.btn_clicked)
def btn_clicked(self):
QMessageBox.about(self,"창","안녕")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
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)