http://d.hatena.ne.jp/mFumi/20141112/1415806010
このサイトをお手本にQWidgetとQGraphicsViewの組み合わせを実現しようとしているのですが、うまくいきません。
ウィンドウ左側に碁盤のマス目(9×9)、右側にボタンを3つ配置しようとしています。
コードも見比べてみましたが、pyqtを触り始めて間もないので良く分かりません。
もう一つ参考にしているサイトは次
コードを貼りますので、どこを直したらよいかご教授お願い致します。
#!/usr/bin/env python from PyQt5.QtCore import (QLineF, QPointF, QRectF, Qt) from PyQt5.QtGui import (QBrush, QColor, QPainter) from PyQt5.QtWidgets import (QWidget, QApplication, QGraphicsView, QGraphicsScene, QGraphicsItem, QGridLayout, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton) import numpy as np class TicTacToe(QGraphicsItem): def __init__(self): super(TicTacToe, self).__init__() self.board = [[-1, -1, -1],[-1, -1, -1], [-1, -1, -1]] self.goban = np.zeros((11, 11)) self.O = 0 self.X = 1 self.turn = self.O self.kuro = 1 self.siro = -1 self.space = 0 self.now = self.kuro def reset(self): for y in range(11): for x in range(11): self.board[y][x] = -1 self.goban[y][x] = 0 self.turn = self.O self.update() def select(self, x, y): if x < 1 or y < 1 or x > 9 or y > 9: return """ if self.board[y][x] == -1: self.board[y][x] = self.turn self.turn = 1 - self.turn """ #とりあえず self.goban[y][x] = self.kuro def paint(self, painter, option, widget): painter.setPen(Qt.black) """ painter.drawLine(0,100,300,100) painter.drawLine(0,200,300,200) painter.drawLine(100,0,100,300) painter.drawLine(200,0,200,300) """ self.goban_left_top_x = 30 self.goban_left_top_y = 30 self.length_of_between = 30 self.goban_line_num = 9 for x in range(self.goban_left_top_x, self.goban_left_top_x + self.length_of_between * self.goban_line_num, self.length_of_between): painter.drawLine(x, self.goban_left_top_y, x, self.goban_left_top_y + self.length_of_between * (self.goban_line_num - 1)) for y in range(self.goban_left_top_y, self.goban_left_top_y + self.length_of_between * self.goban_line_num, self.length_of_between): painter.drawLine(self.goban_left_top_x, y, self.goban_left_top_x + self.length_of_between * (self.goban_line_num - 1), y) """ for y in range(3): for x in range(3): if self.board[y][x] == self.O: painter.setPen(Qt.red) painter.drawEllipse(QPointF(50+x*100, 50+y*100), 30, 30) elif self.board[y][x] == self.X: painter.setPen(Qt.blue) painter.drawLine(20+x*100, 20+y*100, 80+x*100, 80+y*100) painter.drawLine(20+x*100, 80+y*100, 80+x*100, 20+y*100) """ for y in range(1, 10): for x in range(1, 10): cross_point_x = self.goban_left_top_x + self.length_of_between * (x - 1) cross_point_y = self.goban_left_top_y + self.length_of_between * (y - 1) if self.goban[y][x] == self.kuro: painter.setPen(Qt.red) painter.drawEllipse(QPointF(cross_point_x, cross_point_y), 10, 10) elif self.goban[y][x] == self.siro: #とりあえず pass def boundingRect(self): return QRectF(0,0,300,300) def mousePressEvent(self, event): pos = event.pos() #self.select(int(pos.x()/100), int(pos.y()/100)) hantei_ryouiki_satan = int(self.goban_left_top_x - int(self.length_of_between / 2)) x_from_hantei_ryouiki_satan = int(pos.x()) - hantei_ryouiki_satan hantei_ryouiki_joutan = int(self.goban_left_top_y - int(self.length_of_between / 2)) y_from_hantei_ryouiki_joutan = int(pos.y()) - hantei_ryouiki_joutan x = int(x_from_hantei_ryouiki_satan / self.length_of_between) + 1 y = int(y_from_hantei_ryouiki_joutan / self.length_of_between) + 1 print("x = {}, y = {}\n".format(x, y)) self.select(x, y) self.update() super(TicTacToe, self).mousePressEvent(event) #class MainWindow(QGraphicsView): class MainWindow(QWidget): def __init__(self): super(MainWindow, self).__init__() self.initUI() def initUI(self): self.graphicsView = QGraphicsView() #scene = QGraphicsScene(self) scene = QGraphicsScene(self.graphicsView) scene.setSceneRect(0, 0, 400, 400) self.graphicsView.setScene(scene) self.tic_tac_toe = TicTacToe() scene.addItem(self.tic_tac_toe) self.kuro_button = QPushButton('黒', self) self.kuro_button.setCheckable(True) self.kuro_button.clicked[bool].connect(self.setColor) #scene.addItem(self.kuro_button) self.siro_button = QPushButton('白', self) self.siro_button.setCheckable(True) self.siro_button.clicked[bool].connect(self.setColor) #scene.addItem(self.siro_button) self.space_button = QPushButton('なし', self) self.space_button.setCheckable(True) self.space_button.clicked[bool].connect(self.setColor) #scene.addItem(self.space_button) vbox = QVBoxLayout() vbox.addWidget(self.kuro_button) vbox.addWidget(self.siro_button) vbox.addWidget(self.space_button) self.setLayout(vbox) #scene.setSceneRect(0, 0, 300, 300) scene.setSceneRect(0, 0, 500, 500) #self.setScene(scene) #self.graphicsView.setScene(scene) #self.setCacheMode(QGraphicsView.CacheBackground) mainLayout = QHBoxLayout() mainLayout.addWidget(self.graphicsView) mainLayout.addLayout(vbox) self.setLayout(mainLayout) self.setWindowTitle("Tic Tac Toe") def keyPressEvent(self, event): key = event.key() if key == Qt.Key_R: self.tic_tac_toe.reset() super(MainWindow, self).keyPressEvent(event) def setColor(self, pressed): source = self.sender() if source.text() == "黒": self.tic_tac_toe.now = self.tic_tac_toe.kuro elif source.text() == "白": self.tic_tac_toe.now = self.tic_tac_toe.siro elif source.text() == "なし": self.tic_tac_toe.now = self.tic_tac_toe.space if __name__ == '__main__': import sys app = QApplication(sys.argv) mainWindow = MainWindow() mainWindow.show() sys.exit(app.exec_())
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。