简体   繁体   中英

Programming a GUI with PyQt in Python3

I'm asked to create a GUI with PyQt without using any Qtdesigner for my assignment. But now I'm facing a problem. In this pic GUI screenshoot

as you can see, there is a spinbox "Anzahl der Schicht". What I want to do is, when the user sets the value for this spinbox, the area which below it will show the corresponding rows of the input(the combination of QLineEdit,QSlider Widget,QLineEdit and 2 QSpinboxes in a row).

For example, the pic that I uploaded, means the value of spinbox "Anzahl der Schicht" is 3, so there are 3 rows below it. If the value is 4, there should be 4 rows. There is no limited value for the spinbox. How can I make this kind of dynamic effect for the GUI?

Update on 05.07.2019 Thanks for all useful replies. The following code and pic GUI version2 is my current status. So far I can't connect the Qspinbox to the class Widget() for adding or deleting the rows. So I just use a button "add widget" to implement what I want.

class ExampleWidget(QtWidgets.QGroupBox):
def __init__(self, numAddWidget):
    QtWidgets.QGroupBox.__init__(self)
    self.numAddWidget = numAddWidget
    self.initSubject()
    self.organize()
    self.setFlat(True)
    self.setStyleSheet("border: 1px solid transparent")

def initSubject(self):
    self.shiftname =QtWidgets.QLineEdit() # Eingabefeld init
    self.shiftname.setText('0')
    self.shiftpercent = QtWidgets.QSlider()
    self.shiftpercent.setOrientation(QtCore.Qt.Horizontal)
    self.carnum =QtWidgets.QLineEdit() # Eingabefeld init
    self.carnum.setText('0')
    self.start = QtWidgets.QTimeEdit()
    self.start.setDisplayFormat("HH:mm")
    self.end = QtWidgets.QTimeEdit()
    self.end.setDisplayFormat("HH:mm")        

def organize(self):
    grid = QtWidgets.QGridLayout(self)
    self.setLayout(grid)
    grid.addWidget(self.shiftname, 0,0)
    grid.addWidget(self.shiftpercent, 0,1)
    grid.addWidget(self.carnum, 0,2)
    grid.addWidget(self.start, 0,3)
    grid.addWidget(self.end, 0,4)


class Widget(QtWidgets.QWidget):
def __init__(self):
    super().__init__()
    self.numAddWidget = 1
    self.initUi()

def initUi(self):
    self.layoutV = QtWidgets.QVBoxLayout(self)

    self.area = QtWidgets.QScrollArea(self)
    self.area.setWidgetResizable(True)
    self.scrollAreaWidgetContents = QtWidgets.QWidget()

    self.layoutH = QtWidgets.QHBoxLayout(self.scrollAreaWidgetContents)
    self.gridLayout = QtWidgets.QGridLayout()
    self.layoutH.addLayout(self.gridLayout)

    self.area.setWidget(self.scrollAreaWidgetContents)
    self.add_button = QtWidgets.QPushButton("Add Widget")
    self.layoutV.addWidget(self.add_button)
    self.layoutV.addWidget(self.area)
    self.add_button.clicked.connect(self.addWidget)

    self.widget = ExampleWidget(self.numAddWidget)
    self.gridLayout.addWidget(self.widget)       

def addWidget(self):
    self.numAddWidget += 1
    self.widget = ExampleWidget(self.numAddWidget)
    self.gridLayout.addWidget(self.widget)

if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_()) 

Here is a way. In this example the widgets ( QLineEdit s in this case) are stored in a list, Widget.items . When the value of the spin box changes extra widgets are appended to this list and added to the vertical layout if necessary. The widgets are then shown or hidden depending whether the current number of visible widgets is greater or smaller than the value of the spin box.

from PyQt5 import QtWidgets

class Widget(QtWidgets.QWidget):
    def __init__(self, *args, **kwargs):
        super().__init__()
        self.resize(500,500)
        self.items = []
        self.item_count = 0
        self.item_factory = QtWidgets.QLineEdit

        group_box = QtWidgets.QGroupBox()
        self.item_layout = QtWidgets.QVBoxLayout(group_box)
        self.item_layout.addStretch(2)

        self.spin_box = QtWidgets.QSpinBox(self)
        self.spin_box.valueChanged.connect(self.set_item_count)

        h_layout = QtWidgets.QHBoxLayout(self)
        h_layout.addWidget(group_box, 2)
        h_layout.addWidget(self.spin_box, 0)


    def set_item_count(self, new_count:int):
        n_items = len(self.items)
        for ii in range(n_items, new_count):
            item = self.item_factory(self)
            self.items.append(item)
            self.item_layout.insertWidget(n_items, item)
        for ii in range(self.item_count, new_count):
            self.item_layout.itemAt(ii).widget().show()
        for ii in range(new_count, self.item_count):
            self.item_layout.itemAt(ii).widget().hide()
        self.item_count = new_count

if __name__ == "__main__":
    app = QtWidgets.QApplication([])
    window = Widget()
    window.show()
    app.exec()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM