Issue
I am trying to write a program in python (PYQT5) with the following requirements:
- The user can press a button that will create another button on the screen.
- The user decides how many buttons there will be displayed on the screen.
- The screen can only contain a maximum of 4 columns.
I can not post my code because i am trying to figure out how i can program this.
Here is a picture of a GUI that i found on gui, basically i want my window to look like this.
I don't know how to program the following requirements:
- When the user is pressing the add-button for the fifth time, the program automatically needs to place the button on the upcoming row (row 2nd).
Do you guys have some idea's or methods to solve this issue?
Solution
It would just be a divmod to get the row and column.
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class Template(QWidget):
def __init__(self):
super().__init__()
add_btn = QPushButton('Add')
add_btn.clicked.connect(self.add_button)
self.grid = QGridLayout(self)
self.grid.addWidget(add_btn, 0, 0, 1, 4, Qt.AlignLeft)
def add_button(self):
i = self.grid.count() - 1 # Subtract 1 for add_btn
self.grid.addWidget(QPushButton(f'{i + 1}\nok'), 1 + i // 4, i % 4) # Add 1 to row since add_btn is on first row
if __name__ == '__main__':
app = QApplication(sys.argv)
gui = Template()
gui.show()
sys.exit(app.exec_())
Answered By - alec
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.