Python: Using Output From Method's Class Inside Another Class
I am trying to develop my first app with PyQt5 (a memory game). I have created two classes: MainApplication, which inherits from QMainWindow, and GridWidget, which inherits from QW
Solution 1:
I think it will be simpler if you keep everything in one class. You should make each child widget an attribute of the class, so you can access them later using self
within the methods of the class. All the program logic will go in the methods - so it's just a matter of calling methods in response to signals and events.
Here is what the class should look like:
class MainApplication(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.statusBar()
openFile = QAction('Open', self)
openFile.setStatusTip('Search image folder')
openFile.triggered.connect(self.showDialog)
menubar = self.menuBar()
self.fileMenu = menubar.addMenu('&File')
self.fileMenu.addAction(openFile)
self.gridWidget = QWidget(self)
self.gridLayout = QGridLayout(self.gridWidget)
self.setCentralWidget(self.gridWidget)
self.setGeometry(300, 300, 350, 300)
self.setWindowTitle('Memory Game!')
self.show()
def populateGrid(self, images):
names = images * 2
positions = [(i,j) for i in range(int(len(names)/2)) for j in range(int(len(names)/2))]
for position, name in zip(positions, names):
if name == '':
continue
button = QPushButton(name)
self.gridLayout.addWidget(button, *position)
def showDialog(self):
folder = str(QFileDialog.getExistingDirectory(self, "Select Directory",
'/home', QFileDialog.ShowDirsOnly))
images = glob.glob(os.path.join(folder, '*.jpg'))
if images:
self.populateGrid(images)
Post a Comment for "Python: Using Output From Method's Class Inside Another Class"