空白PyQt5计划

时间:2017-05-09 15:09:45

标签: python user-interface pyqt pyqt5

我尝试使用PyQt5创建一个简单的GUI,但每当我运行该程序时。它显示的只是一个空白窗口。如何实现按钮或QLineEdit等元素实际显示在窗口中?

任何指针都会受到赞赏,因为我正在尝试创建一个可以接受用户输入的应用程序(在这种情况下是徽章和票号),然后在将信息显示为输出之前查询数据库中的管属性和数量。

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 个答案:

答案 0 :(得分:-1)

在这个小小的例子上看一下,尽量做到最简单。还有更多要学习的东西和一些更好的做法,不仅仅是通过这样的一切,但无论如何,这里是:

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_())

您将看到,稍后您将必须覆盖从qt类继承的许多类,并根据您的需要逐个进行个性化。只是提示")