Installation[edit | edit source]
To install Qt Designer without having to download all the GBs of data (on Windows) simply go into Anaconda and type the following :
pip install pyqt5-installer pip install pyqt5 pip install pyqt5-tools
Once the installations are finished go into the pyqt5 site-packages folder, for me the address for this was :
And in there you will find the designer.exe application file. Click on that and Qt Designer will start.
Creating our first basic app[edit | edit source]
Now that you are in Qt lets select the template 'Dialog with Buttons Bottom'.
You can change your UI in here, but for now lets just change the Window Title and then save our ui as dialog.ui.
Converting .ui to .py[edit | edit source]
Go back into Anaconda and go to where our ui file is that we just created. Then enter :
pyuic5 dialog.ui > dialog.py
This will convert our ui file into a py file that we can actually use.
Next we can make some minor changes such as the generated class name so it makes more sense to us.
The final code should look something like this :
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'magicCounter_UI.ui' # # Created by: PyQt5 UI code generator 5.11.3 # # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Dialog(object): def setupUi(self, Dialog): Dialog.setObjectName("Dialog") Dialog.resize(400, 300) self.buttonBox = QtWidgets.QDialogButtonBox(Dialog) self.buttonBox.setGeometry(QtCore.QRect(30, 240, 341, 32)) self.buttonBox.setOrientation(QtCore.Qt.Horizontal) self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok) self.buttonBox.setObjectName("buttonBox") self.retranslateUi(Dialog) self.buttonBox.accepted.connect(Dialog.accept) self.buttonBox.rejected.connect(Dialog.reject) QtCore.QMetaObject.connectSlotsByName(Dialog) def retranslateUi(self, Dialog): _translate = QtCore.QCoreApplication.translate Dialog.setWindowTitle(_translate("Dialog", "Magic: The Gathering Life Counter"))
Then we create another .py file and call it app.py :
import sys from PyQt5.QtWidgets import QDialog, QApplication from dialog import Ui_Dialog class AppWindow(QDialog): def __init__(self): super().__init__() self.ui = Ui_Dialog() self.ui.setupUi(self) self.show() app = QApplication(sys.argv) w = AppWindow() w.show() sys.exit(app.exec_())
Then we run our app.py file and we should get our window looking something like this :
If you are using the template of a MainWindow then your app.py file will need to look slightly different, otherwise you will get this pesky
AttributeError: 'AppWindow' object has no attribute 'setCentralWidget'
error. So instead we need to implement it with QMainWIndow.
To do this, your PyQt5.QtWidgets import statement should look something like this :
from PyQt5.QtWidgets import QApplication, QMainWindow
and then down the bottom where you declared your app and window variables, change and add the following :
app = QApplication(sys.argv) window = QMainWindow() ui = Ui_MainWindow() ui.setupUi(window) window.show() sys.exit(app.exec_())
Now when you run the app.py file, it should load successfully.
References[edit | edit source]
Design Simple Dialog Using PyQt5 Designer Tool - Codementor Community
AttributeError: QDialog object has no attribute setCentralWidget - Stackoverflow