Dec-26-2020, 07:11 PM
Hello everybody!
I am working on my homework and I have a problem with QPushButtons. I need to make a field(8*8) of QPushButtons and then work with every button seperately. For example, if I click on button, I want it to turn red, but in my code it works only for last button. I know that it probably has simply solution, but I am beginner and I have already spent quite long time on that. Thans for ideas :D
from PyQt5.QtWidgets import *
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.resize(100,300)
layout = QGridLayout()
for x in range(8):
for y in range(8):
self.buttons = QPushButton(self)
layout.addWidget(self.buttons,x,y)
self.setLayout(layout)
self.buttons.clicked.connect(self.flag)
def flag(self):
self.buttons.setStyleSheet("background-color : red")
if __name__ == '__main__':
app = QApplication([])
window = MainWindow()
window.show()
app.exec()
I am working on my homework and I have a problem with QPushButtons. I need to make a field(8*8) of QPushButtons and then work with every button seperately. For example, if I click on button, I want it to turn red, but in my code it works only for last button. I know that it probably has simply solution, but I am beginner and I have already spent quite long time on that. Thans for ideas :D
from PyQt5.QtWidgets import *
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.resize(100,300)
layout = QGridLayout()
for x in range(8):
for y in range(8):
self.buttons = QPushButton(self)
layout.addWidget(self.buttons,x,y)
self.setLayout(layout)
self.buttons.clicked.connect(self.flag)
def flag(self):
self.buttons.setStyleSheet("background-color : red")
if __name__ == '__main__':
app = QApplication([])
window = MainWindow()
window.show()
app.exec()