Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PyQt5 import QtWidgets, QtCore, QtGui
- import string
- grid = [[["text", "GREEN"], 0, ["text", "RED"], 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0]]
- BTN = """QPushButton{{font-weight: bold; color: {};
- font-size: 14px; background-color: {};
- border-width: 2px; border-radius: 100px}}"""
- class Widget(QtWidgets.QWidget):
- def __init__(self, parent=None):
- super(Widget, self).__init__(parent)
- self.initUI()
- self.createLayout()
- self.center_widget()
- def initUI(self):
- p = self.palette()
- gradient = QtGui.QLinearGradient(0, 0, 0, 400)
- gradient.setColorAt(0.0, QtGui.QColor('#f1f1f1'))
- gradient.setColorAt(1.0, QtGui.QColor('#00a1de'))
- p.setBrush(QtGui.QPalette.Window, QtGui.QBrush(gradient))
- self.setPalette(p)
- def createLayout(self):
- hlay = QtWidgets.QHBoxLayout(self)
- frameL = QtWidgets.QFrame()
- vlay = QtWidgets.QVBoxLayout(frameL)
- frame = QtWidgets.QFrame()
- frame.setObjectName("principal")
- frame.setStyleSheet("#principal{border: 2px solid white;}")
- hlay.addWidget(frameL)
- hlay.addWidget(frame)
- gridLayout = QtWidgets.QGridLayout(frame)
- h = 60 # height
- for i, row in enumerate(grid):
- letter = "{}".format(string.ascii_uppercase[i])
- frameButton = QtWidgets.QFrame()
- frameButton.setFixedHeight(h)
- frameButton.setContentsMargins(0, 0, 0, 0)
- lay = QtWidgets.QVBoxLayout(frameButton)
- button = QtWidgets.QPushButton(letter)
- button.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
- button.clicked.connect(lambda: self.populate_row(letter)) # Every button click returns 'J'
- lay.addWidget(button)
- vlay.addWidget(frameButton)
- for j, val in enumerate(row):
- gridButton = QtWidgets.QPushButton()
- gridButton.setFixedSize(h, h)
- if isinstance(val, list):
- gridButton.setText(val[0])
- if val[1] == "GREEN":
- gridButton.setStyleSheet(BTN.format('white', 'green'))
- elif val[1] == "RED":
- gridButton.setStyleSheet(BTN.format('white', 'red'))
- else:
- gridButton.setStyleSheet(BTN.format('black', 'white'))
- gridButton.setText("{}".format(val))
- gridLayout.addWidget(gridButton, i, j)
- for ix in range(j + 1):
- label = QtWidgets.QLabel("{}".format(ix + 1))
- label.setAlignment(QtCore.Qt.AlignCenter)
- gridLayout.addWidget(label, i + 1, ix)
- vlay.addWidget(QtWidgets.QLabel())
- def center_widget(self):
- self.window().setGeometry(
- QtWidgets.QStyle.alignedRect(
- QtCore.Qt.LeftToRight,
- QtCore.Qt.AlignCenter,
- self.window().size(),
- QtWidgets.QApplication.desktop().availableGeometry())
- )
- def populate_row(self, letter):
- """ Populate the row with data """
- print(letter)
- if __name__ == '__main__':
- import sys
- app = QtWidgets.QApplication(sys.argv)
- w = Widget()
- w.show()
- sys.exit(app.exec_())
Add Comment
Please, Sign In to add comment