PyQt垂直布局按钮堆叠

时间:2020-08-18 09:55:09

标签: python layout pyqt5 qt-designer

我正在尝试为我的一个路由器配置器创建一个GUI。我在QtDesigner中创建了以下窗口

Window created in QtDesigner

对话框布局是垂直布局

pyuic5生成以下输出

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.15.0
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(336, 339)
        self.verticalLayout = QtWidgets.QVBoxLayout(Dialog)
        self.verticalLayout.setObjectName("verticalLayout")
        self.label = QtWidgets.QLabel(Dialog)
        self.label.setObjectName("label")
        self.verticalLayout.addWidget(self.label)
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.lineEdit = QtWidgets.QLineEdit(Dialog)
        self.lineEdit.setObjectName("lineEdit")
        self.horizontalLayout_2.addWidget(self.lineEdit)
        self.pushButton_2 = QtWidgets.QPushButton(Dialog)
        self.pushButton_2.setObjectName("pushButton_2")
        self.horizontalLayout_2.addWidget(self.pushButton_2)
        self.verticalLayout.addLayout(self.horizontalLayout_2)
        self.label_3 = QtWidgets.QLabel(Dialog)
        self.label_3.setObjectName("label_3")
        self.verticalLayout.addWidget(self.label_3)
        self.tableWidget = QtWidgets.QTableWidget(Dialog)
        self.tableWidget.setRowCount(0)
        self.tableWidget.setColumnCount(0)
        self.tableWidget.setObjectName("tableWidget")
        self.verticalLayout.addWidget(self.tableWidget)
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.label_2 = QtWidgets.QLabel(Dialog)
        self.label_2.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignTop)
        self.label_2.setObjectName("label_2")
        self.horizontalLayout.addWidget(self.label_2)
        self.pushButton = QtWidgets.QPushButton(Dialog)
        self.pushButton.setObjectName("pushButton")
        self.horizontalLayout.addWidget(self.pushButton)
        self.verticalLayout.addLayout(self.horizontalLayout)
        self.pushButton_3 = QtWidgets.QPushButton(Dialog)
        self.pushButton_3.setObjectName("pushButton_3")
        self.verticalLayout.addWidget(self.pushButton_3)
        self.pushButton_3.raise_()
        self.tableWidget.raise_()
        self.label_3.raise_()
        self.label.raise_()

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

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Router Configurator"))
        self.label.setText(_translate("Dialog", "Project Number"))
        self.lineEdit.setToolTip(_translate("Dialog", "Projekt Nummer eingeben"))
        self.pushButton_2.setText(_translate("Dialog", "Download"))
        self.label_3.setText(_translate("Dialog", "Found Setup"))
        self.label_2.setText(_translate("Dialog", "Directory"))
        self.pushButton.setText(_translate("Dialog", "Change Directory"))
        self.pushButton_3.setText(_translate("Dialog", "Generate Config"))

运行程序时,按钮显示为堆叠在一起,而不是垂直排列。

Window as it appears when running the program

Windows
QtDesigner 5.11.1
Python 3.7

主要代码

from QtDesigner import Ui_Dialog
from PyQt5 import QtGui,QtCore, QtWidgets
import sys
from SQLhandler import get_sql_data

class gui(QtWidgets.QMainWindow, Ui_Dialog):

    def __init__(self):
        super(gui, self).__init__()
        self.setupUi(self)
        self.pushButton.clicked.connect(self.change_directory)
        self.pushButton_2.clicked.connect(self.get_data)

    #change the directory where config files will be stored
    def change_directory(self):
        dlg = QtWidgets.QFileDialog()
        dlg.setFileMode(QtWidgets.QFileDialog.Directory)

        if dlg.exec_():
            self.path_directory = dlg.selectedFiles()
            self.label_2.setText("directory for config:\n{}/project".format(self.path_directory[0]))

        self.directory_set = True

    #get turbine data
    def get_data(self):
        self.project_id = self.lineEdit.text()
        self.project_data = get_sql_data(self.project_id)
        self.turbine_data = self.project_data[0]
        self.turbine_count = self.project_data[1]
        self.tableWidget.setColumnCount(2)
        self.tableWidget.setRowCount(self.turbine_count)
        self.tableWidget.setHorizontalHeaderLabels(['Config1', 'Config2'])
        #self.tableWidget.setSizeAdjustPolicy(QtWidgets.QAbstractScrollArea.AdjustToContents)

        #fill table
        element = 0
        for row in self.turbine_data:
            if self.turbine_data[element][0] == 1:
                type_avlight = 'no forwarding'
            elif self.turbine_data[element][0] == 2:
                type_avlight = 'forwarding 1'
            elif self.turbine_data[element][0] == 3:
                type_avlight = 'forwarding 2'
            elif self.turbine_data[element][0] == 4:
                type_avlight = 'forwarding 3'
            elif self.turbine_data[element][0] == 5:
                type_avlight = 'forwarding 4'
            elif self.turbine_data[element][0] == 6:
                type_avlight = '5'
            else:
                type_avlight = 'unvalid'

            if self.turbine_data[element][1] == 1:
                plc = 'secondary PLC'
            else:
                plc = 'no secondary PLC'


            self.tableWidget.setItem(element, 0, QtWidgets.QTableWidgetItem(type_avlight))
            self.tableWidget.setItem(element, 1, QtWidgets.QTableWidgetItem(plc))
            element = element + 1

        self.tableWidget.resizeColumnsToContents()

    def create_directory(self):
        0

#https://stackoverflow.com/questions/22069321/realtime-output-from-a-subprogram-to-stdout-of-a-pyqt-widget
#https://www.tutorialspoint.com/pyqt/pyqt_qformlayout_class.htm

#Function Main Start
def main():
    app = QtWidgets.QApplication(sys.argv)
    window=gui()
    window.show()
    sys.exit(app.exec_())
#Function Main END

if __name__ == '__main__':
    main()

0 个答案:

没有答案