2016-09-25 10 views
0

私はPyQt5(メモリゲーム)で私の最初のアプリを開発しようとしています。python:別のクラスのメソッドクラスからの出力を使用

私は、2つのクラスを作成している:QMainWindowから継承MainApplication、およびQWidgetから継承GridWidgetを、。私の目的は、menuBarのfileMenuを使っていくつかの画像(jpg)を持つフォルダをユーザに指定させることです。

したがって、MainApplicationでIはFILEMENUに接続し、リスト変数に格納されたファイル名のリスト(選択したフォルダ内の画像の名前)、出力方法にShowDialogを作成しました。これをグリッドを作成して塗りつぶすことができるように、GridWidgetに渡したいと思います。

私はOOPプログラミングの初心者です。私のスクリプトの構成は最高ではないかもしれません。私はそれを改善するための提案をしています。私の考えはGridWidgetMainApplicationに追加することでしたが、showDialogの出力をこれに渡す方法はわかりません。どんな提案もありがとうございます。

はここに、これまでに私のコードです:

#!/usr/bin/python3 
# -*- coding: utf-8 -*- 

""" 
Memory game 

My first memory game in PyQt5. 

author: Umberto Minora 
last edited: September 2016 
""" 

import os, sys, glob 
from PyQt5.QtWidgets import (QMainWindow, QWidget, 
    QGridLayout, QPushButton, QApplication, 
    QAction, QFileDialog) 
from PyQt5.QtGui import QPixmap 

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() 
     fileMenu = menubar.addMenu('&File') 
     fileMenu.addAction(openFile) 

     self.form_widget = GridWidget(self) 

     self.setGeometry(300, 300, 350, 300) 
     self.setWindowTitle('Memory Game!') 
     self.show() 

    def showDialog(self): 

     folder = str(QFileDialog.getExistingDirectory(self, "Select Directory", 
      '/home', QFileDialog.ShowDirsOnly)) 

     images = glob.glob(os.path.join(folder, '*.jpg')) 

     if images: 
      return images * 2 

class GridWidget(QWidget): 

    def __init__(self): 
     super().__init__() 

     grid = QGridLayout() 
     self.setLayout(grid) 

     names = self.showDialog() # DA SISTEMARE!!!!!! 

     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) 
      grid.addWidget(button, *position) 

if __name__ == '__main__': 

    app = QApplication(sys.argv) 
    ex = MainApplication() 
    sys.exit(app.exec_()) 

EDIT @ekhumoroの答えに

おかげで、今のコードは動作しています。ここに私が実際に実行しているコードがあります(これは完全なゲームではなく、単にフォルダから画像を取り込むだけです)。

#!/usr/bin/python3 
# -*- coding: utf-8 -*- 

""" 
Memory game 2 

My first memory game in PyQt5. 

author: Umberto Minora 
last edited: September 2016 
""" 

import os, sys, glob, math 
from PyQt5.QtWidgets import (QMainWindow, QWidget, 
    QGridLayout, QPushButton, QApplication, 
    QAction, QFileDialog, QLabel) 
from PyQt5.QtGui import QPixmap 

class MainApplication(QMainWindow): 
    def __init__(self): 
     super().__init__() 
     self.initUI() 

    def initUI(self): 
     self.statusBar() 

     openFile = QAction('Open', self) 
     openFile.setShortcut('Ctrl+O') 
     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 
     n_cols = math.ceil(math.sqrt(len(names))) 
     n_rows = math.ceil(math.sqrt(len(names))) 
     positions = [(i,j) for i in range(n_cols) for j in range(n_rows)] 
     for position, name in zip(positions, names): 
      if name == '': 
       continue 
      pixmap = QPixmap(name) 
      scaled = pixmap.scaled(pixmap.width()/3, pixmap.height()/3) 
      del(pixmap) 
      lbl = QLabel(self) 
      lbl.setPixmap(scaled) 
      self.gridLayout.addWidget(lbl, *position) 

    def showDialog(self): 
     folder = str(QFileDialog.getExistingDirectory(self, "Select Directory", 
      '.', QFileDialog.ShowDirsOnly)) 

     images = glob.glob(os.path.join(folder, '*.jpg')) 

     if images: 
      self.populateGrid(images) 


if __name__ == '__main__': 

    app = QApplication(sys.argv) 
    ex = MainApplication() 
    sys.exit(app.exec_()) 

答えて

1

すべてを1つのクラスにまとめると簡単になると思います。クラスのメソッド内でselfを使用して後でアクセスできるように、各子にクラスの属性をウィジェットにする必要があります。すべてのプログラムロジックがメソッドに入ります。シグナルやイベントに応答してメソッドを呼び出すだけです。

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) 
:ここ

は、クラスがどのように見えるかです

関連する問題