Python Forum

Full Version: Attempting to use Qt Designer.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hey all I'm trying to make a simple GUI using QT Designer and having some trouble. This is going to be a simple app to keep up with hardware and software inventory at work.

I'm not totally new to programming. Took C++, C# and Java classes back in the early 2000s when .NET was still new so it's definitely been a while.

I'm using PyCharm. I created the GUI in Qt Designer and saved the .ui file in my project directory. I then used my PyQt5 external tool to convert it to .py. It loads the code below and of course it won't run. I did attempt to customize the code myself and managed to get rid of all errors but I think I hacked it up so bad it still wouldn't load.

I'm a little confused since when I watch tutorials of people doing exactly what I did they don't have to change the coding at all. It runs as soon as they convert it from ui to py. Any advice or guidance would be great. I don't mind customizing the code but I feel like I shouldn't have to seeing that other people aren't having to.

[Image: forum3.jpg]

[Image: forum2.jpg]
Ahhh getting closer...

Constructor

[Image: forum4.jpg]
Missed importing PyQt5 QtCore. Still trying to figure out the retranslateUi function.


[Image: forum5.jpg]
Please post code and error message, not a screen image. If your code is large, make a simpler example that has a similar problem. Often I find making the simpler example helps me solve my original problem.
Totally didn't see the insert python button.

Line 46. Parameter "MainWindow" unfilled.

ex = Ui_MainWindow()

I'm going to look at the code some more tomorrow with fresh eyes. I'm not entirely sure why I need the top if statement. I was thinking I would place "object" as the parameter here but no go. I'll figure it out in the morning.

from PyQt5 import QtWidgets, QtCore
from PySide2.QtCore import *
from PySide2.QtWidgets import *
import sys


class Ui_MainWindow(object):
    def __init__(self, MainWindow):
        if not MainWindow.objectName():
            MainWindow.setObjectName(u"MainWindow")
            MainWindow.resize(327, 196)
            MainWindow.setStyleSheet(u"background-color: rgb(255, 255, 255);")
            self.CentralWidget = QWidget(MainWindow)
            self.CentralWidget.setObjectName(u"centralized")
            self.label = QLabel(self.CentralWidget)
            self.label.setObjectName(u'label')
            self.label.setGeometry(QRect(10, 0, 151, 181))
            self.hardware_button = QPushButton(self.CentralWidget)
            self.hardware_button.setObjectName(u"hardware_button")
            self.hardware_button.setGeometry(QRect(190, 10, 121, 51))
            self.software_button = QPushButton(self.CentralWidget)
            self.software_button.setObjectName(u"software_button")
            self.software_button.setGeometry(QRect(190, 70, 121, 51))
            self.reporting_button = QPushButton(self.CentralWidget)
            self.reporting_button.setObjectName(u"reporting_button")
            self.reporting_button.setGeometry(QRect(190, 130, 121, 41))
            MainWindow.setCentralWidget(self.CentralWidget)
            self.statusbar = QStatusBar(MainWindow)
            self.statusbar.setObjectName(u"statusbar")
            MainWindow.setStatusBar(self.statusbar)

            self.retranslateUi(MainWindow)
            QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(QCoreApplication.translate(MainWindow, "Hardware/Software Manager"))
        self.label.setText(QCoreApplication.translate(MainWindow, "Hardware/Software Manager", None))
        self.hardware_button.setText(QCoreApplication.translate(MainWindow, u"Hardware"))
        self.software_button.setText(QCoreApplication.translate(MainWindow, u"Software"))
        self.reporting_button.setText(QCoreApplication.translate(MainWindow, u"Reporting"))


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    ex = Ui_MainWindow() 
    w = QtWidgets.QMainWindow() 
    ex.__init__(w)
    w.show() 
    sys.exit(app.exec_())
from PyQt5 import QtWidgets, QtCore
from PySide2.QtCore import *
from PySide2.QtWidgets import *
Why this?. Do you use PyQt5 or PySide2?
without designer (but it would be better to use a layout, not setGeometry on Widgets)

from PyQt5.QtWidgets import (QMainWindow, QApplication, 
                              QPushButton, QWidget, 
                              QStatusBar, QLabel)
from PyQt5.QtCore import QRect
import sys
 
 
class Ui_MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(Ui_MainWindow, self).__init__()
        
        self.setStyleSheet("QMainWindow {background-color: #eee;} \
                            QStatusBar {font-size: 8pt; color: #444;}")
        self.label = QLabel("Label", self)
        self.label.setGeometry(QRect(10, 0, 151, 181))
        self.hardware_button = QPushButton("Hardware", self)
        self.hardware_button.clicked.connect(self.hardware_button_clicked)
        self.hardware_button.setGeometry(QRect(190, 10, 121, 51))
        self.software_button = QPushButton("Software", self)
        self.software_button.setGeometry(QRect(190, 70, 121, 51))
        self.reporting_button = QPushButton("Reporting", self)
        self.reporting_button.setGeometry(QRect(190, 130, 121, 51))
        self.statusBar = QStatusBar()
        self.setStatusBar(self.statusBar)
        self.statusBar.showMessage("Welcome", -1)
        self.resize(327, 196)
        
    def hardware_button_clicked(self):
        print("hardware_button_clicked")

 
if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Ui_MainWindow() 
    window.show() 
    sys.exit(app.exec_())
Appreciate it Axel.

I'm going to build off your code and customize it. I had a logo image for the label and when I exported the ui file out of Designer the first time it exported a rsc file with the logo image as well but it's not doing that anymore so I need to figure that out and import the logo image.

I was getting ahead of myself and need to slow down. I understand most of the code but I just need to play around with it and customize to get a better feel.

I'm bad about attempting to fly before even crawling at times. I was thinking I could just come in and make a working crud app within an hour or two with the extremely fuzzy programming knowledge I do have. I was wrong.


(Feb-13-2021, 10:23 AM)Axel_Erfurt Wrote: [ -> ]without designer (but it would be better to use a layout, not setGeometry on Widgets)

from PyQt5.QtWidgets import (QMainWindow, QApplication, 
                              QPushButton, QWidget, 
                              QStatusBar, QLabel)
from PyQt5.QtCore import QRect
import sys
 
 
class Ui_MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(Ui_MainWindow, self).__init__()
        
        self.setStyleSheet("QMainWindow {background-color: #eee;} \
                            QStatusBar {font-size: 8pt; color: #444;}")
        self.label = QLabel("Label", self)
        self.label.setGeometry(QRect(10, 0, 151, 181))
        self.hardware_button = QPushButton("Hardware", self)
        self.hardware_button.clicked.connect(self.hardware_button_clicked)
        self.hardware_button.setGeometry(QRect(190, 10, 121, 51))
        self.software_button = QPushButton("Software", self)
        self.software_button.setGeometry(QRect(190, 70, 121, 51))
        self.reporting_button = QPushButton("Reporting", self)
        self.reporting_button.setGeometry(QRect(190, 130, 121, 51))
        self.statusBar = QStatusBar()
        self.setStatusBar(self.statusBar)
        self.statusBar.showMessage("Welcome", -1)
        self.resize(327, 196)
        
    def hardware_button_clicked(self):
        print("hardware_button_clicked")

 
if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Ui_MainWindow() 
    window.show() 
    sys.exit(app.exec_())
save the logo image in the script folder

replace logo.png with the name of your logo (line 18)

from PyQt5.QtWidgets import (QMainWindow, QApplication, 
                              QPushButton, QWidget, 
                              QStatusBar, QLabel)
from PyQt5.QtCore import QRect
from PyQt5.QtGui import QPixmap
import sys
  
  
class Ui_MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(Ui_MainWindow, self).__init__()
         
        self.setStyleSheet("QMainWindow {background-color: #eee;} \
                            QStatusBar {font-size: 8pt; color: #444;}")
                            
        # label image
        self.label = QLabel(self, scaledContents=True)
        pixmap = QPixmap('logo.png')
        self.label.setPixmap(pixmap)
        
        self.label.setGeometry(QRect(10, 0, 151, 181))
        self.hardware_button = QPushButton("Hardware", self)
        self.hardware_button.clicked.connect(self.hardware_button_clicked)
        self.hardware_button.setGeometry(QRect(190, 10, 121, 51))
        self.software_button = QPushButton("Software", self)
        self.software_button.setGeometry(QRect(190, 70, 121, 51))
        self.reporting_button = QPushButton("Reporting", self)
        self.reporting_button.setGeometry(QRect(190, 130, 121, 51))
        self.statusBar = QStatusBar()
        self.setStatusBar(self.statusBar)
        self.statusBar.showMessage("Welcome", -1)
        self.resize(327, 196)
         
    def hardware_button_clicked(self):
        print("hardware_button_clicked")
 
  
if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Ui_MainWindow() 
    window.show() 
    sys.exit(app.exec_())
I actually thought it through and decided to use tab widgets to navigate between the hardware and software manager sections.

In order to do that I have to import QWidget and QTabWidget it looks like. If I am understanding this correctly the QTabWidget needs to be placed on top of the QWidget. Then everything else like labels and buttons have to be placed on the QTabWidget. This is what I have so far, am I going in the right direction?

I want to make sure if I understand lines 8-16 correctly. Please refer to notes in code. Thank you!

from PyQt5.QtWidgets import (QMainWindow, QApplication, QPushButton, QWidget, QStatusBar, QLabel, QTabWidget,
                             QCheckBox, QPlainTextEdit)
from PyQt5.QtCore import QRect
import sys


class mainWindowGUI(QMainWindow):
    def __init__(self):
        super(mainWindowGUI, self).__init__()  # constructs the main window of the app and names it "mainWindowGUI"
        self.setStyleSheet("QMainWindow {background-color: #eee;} \
                            QStatusBar {font-size: 8pt; color: #444;}")  # simply sets color and font settings
        self.main_widget = QWidget(self.mainWindowGUI)  # constructs a QWidget called "main_widget" and places it on "mainWindowGUI"
        self.main_widget.setObjectName("mainWidget")  # renames the QWidget "main_widget" to "mainWidget". Don't think this is necessary?
        self.tab_widget = QTabWidget(self.mainWidget)  # constructs a QTabWidget named "tab_widget" and places it on "mainWidget"
        self.tab_widget.setObjectName("tabWidget")  # renames the QTabWidget "tab_widget" to "tabWidget". Again may not be necessary?
        self.tab_widget.setGeometry(QRect(10, 10, 431, 241))  # sets the size of the QTabWidget "tabWidget"
        self.sn_hw_label = QLabel("SERIAL NUMBER", self)  # creates Serial Number Label for Hardware Tab
        self.sn_hw_label.setGeometry(QRect(20, 20, 131, 31))

    def searchEditHWfunction(self):
        print("Search/Edit Hardware Button Clicked")

    def clearFieldsHWfunction(self):
        print("Clear Fields Hardware Button Clicked")

    def enterNewHWfunction(self):
        print("Enter New Hardware Button Clicked")

    def searchEditSWfunction(self):
        print("Search/Edit Software Button Clicked")

    def clearFieldsSWfunction(self):
        print("Clear Fields Software Button Clicked")

    def enterNewSWfunction(self):
        print("Enter New Softwarw Button Clicked")


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = mainWindowGUI()
    window.show()
    sys.exit(app.exec_())
Pages: 1 2