0
votes

I'm trying to create a simple GUI using PyQt5, but whenever I run the program. all it displays is a blank window. How can get elements such as a pushbutton or the QLineEdit to actually appear in the window?

Any pointers would be appreciated as I am trying to create an application that can take a user input (a badge and ticket number in this case) then query a database for the tube properties and quantities before displaying the information as an output.

from PyQt5 import QtCore, QtWidgets

class Ui_MainWindow(object):

    def __init__(self):
        self.title = 'Tube Bender UI'
        self.description = QtWidgets.QLabel('New Order Available')
        self.badge = QtWidgets.QLineEdit()
        self.ticket = QtWidgets.QLineEdit()
        self.tubes = 0

    def setupUi(self, MainWindow):

        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 641)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
        MainWindow.setSizePolicy(sizePolicy)
        MainWindow.setMaximumSize(QtCore.QSize(800, 641))

        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setMinimumSize(QtCore.QSize(800, 641))
        self.centralwidget.setMaximumSize(QtCore.QSize(800, 641))
        self.centralwidget.setObjectName("centralwidget")
        self.horizontalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
        self.horizontalLayoutWidget.setGeometry(QtCore.QRect(0, 320, 801, 641))

        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.horizontalLayoutWidget.sizePolicy().hasHeightForWidth())

    #tubes = 0

    def coil_id(self):
        QtWidgets.QLineEdit(self)
        return self.ticket

    def page_description(self):
        QtWidgets.QLineEdit(self)
        return self.description

    def page_title(self):
        QtWidgets.QLineEdit(self)
        return self.title

    def badge_id(self):
        QtWidgets.QLineEdit(self)
        return self.badge

    def tube_count(self):
        QtWidgets.QLineEdit(self)
        print(self.tubes)


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    MainPage = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show())
    sys.exit(app.exec_()
1
Absolutely no need to mention your noobishness. We'll either figure it out quickly from your question/code or we won't because you're better than you think. And being a noob shouldn't prevent you from asking good questions, so it really doesn't matter.Mad Physicist
What is the purpose of MainPage = QtWidgets.QMainWindow()?Mad Physicist
I don't think you have added your QLineEdit and QPushButton to the Windowtushortz
You never add the elements you create to the main window.Mad Physicist
Also, what is the purpose of having methods that do QtWidgets.QLineEdit(self)? That just creates a new object every time, and doesn't even destroy it because you have assigned it a parent and thus transferred ownership to Qt.Mad Physicist

1 Answers

-1
votes

Just have this small look on this tiny example, tried to be the simplest I could. There are much more to learn and some much more good practices not just through everything like that, but anyways, here it is:

import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QHBoxLayout
from PyQt5.QtWidgets import QLabel
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QWidget


class MainWindow(QMainWindow):

    layout_main = None
    central_widget = None
    lbl_name = None
    lbl_value = None
    input_name = None
    input_value = None

    def __init__(self):
        super(MainWindow, self).__init__()
        self.init_ui()

    def init_ui(self):
        #variables
        self.layout_main = QVBoxLayout()
        self.layout_labels = QHBoxLayout()
        self.layout_inputs = QHBoxLayout()
        self.central_widget = QWidget()
        self.lbl_name = QLabel("name")
        self.lbl_name.setFixedSize(100,50)
        self.lbl_value = QLabel("Value")
        self.lbl_value.setFixedSize(100, 50)
        self.input_name = QLineEdit()
        self.input_name.setFixedSize(100, 50)
        self.input_value = QLineEdit()
        self.input_value.setFixedSize(100, 50)
        #style
        self.layout_main.setContentsMargins(10, 10, 10, 10)
        self.setMinimumSize(250,100)
        #adding objects
        self.layout_labels.addWidget(self.lbl_name)
        self.layout_labels.addWidget(self.lbl_value)
        self.layout_main.addLayout(self.layout_labels)
        self.layout_inputs.addWidget(self.input_name)
        self.layout_inputs.addWidget(self.input_value)
        self.layout_main.addLayout(self.layout_inputs)

        self.central_widget.setLayout(self.layout_main)
        self.setCentralWidget(self.central_widget)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    mw = MainWindow()
    mw.show()
    sys.exit(app.exec_())

You will see that later you will have to override many classes inheriting from qt classes and personalize one by one according to your need. just a tip ")