Python Forum
PyQt5 QPushButton Problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PyQt5 QPushButton Problem
#1
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()
Reply


Messages In This Thread
PyQt5 QPushButton Problem - by Hellmut - Dec-26-2020, 07:11 PM
RE: PyQt5 QPushButton Problem - by deanhystad - Dec-26-2020, 07:56 PM
RE: PyQt5 QPushButton Problem - by Hellmut - Dec-26-2020, 08:41 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Math problem in Python - pyqt5 rwahdan 6 5,772 Jun-18-2019, 08:11 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020