pyqt5で簡単なお絵描きツール作成をしています。
下図のようなレイアウトを目標としコーディングしましたが、
TypeError: addWidget(self, QWidget, stretch: int = 0, alignment: Union[Qt.Alignment, Qt.AlignmentFlag] = Qt.Alignment()): argument 1 has unexpected type 'QVBoxLayout'
というエラーが吐かれました。
QhboxlayoutにQvboxlayoutを代入していることが原因だと思われますが
検索してもこれと言った解決方法が見当たりませんでした。
原因と解決をご教授お願いしたいです。よろしくお願いいたします。
環境:Pyhton3 windows10
(コードが長すぎるため関係のないであろう箇所は割愛しています)
error
1Traceback (most recent call last): 2 File "paint.py", line 384, in <module> 3 main() 4 File "paint.py", line 377, in main 5 ex = MainWindow() 6 File "paint.py", line 31, in __init__ 7 self.setCentralWidget(self.initUI()) 8 File "paint.py", line 56, in initUI 9 hbox.addWidget(layoutA) 10TypeError: addWidget(self, QWidget, stretch: int = 0, alignment: Union[Qt.Alignment, Qt.AlignmentFlag] = Qt.Alignment()): argument 1 has unexpected type 'QVBoxLayout'
python
1import sys 2from PyQt5.QtWidgets import * 3from PyQt5.QtGui import QPainter, QImage, QPen, qRgb 4from PyQt5.QtCore import Qt, QPoint, QRect, QSize, QDir 5from PyQt5.QtCore import pyqtSlot, pyqtSignal,Qt, QTimer 6from PyQt5.QtGui import QIcon 7from collections import deque 8from PyQt5 import QtCore 9 10import os 11import time 12import concurrent.futures 13import threading 14import torch 15import datetime 16from parameter import * 17from data_loader import Data_Loader 18 19import torch.nn as nn 20from torch.autograd import Variable 21from torchvision.utils import save_image 22 23from sagan_models import Generator, Encoder 24from utils import * 25 26 27class MainWindow(QMainWindow): 28 def __init__(self): 29 super(MainWindow, self).__init__() 30 self.setupUI() 31 self.setCentralWidget(self.initUI()) 32 #クラスを呼び出す。 33 34 def initUI(self): 35 #self.fileName= './test/test/aa.png' 36 self.predict_res_fileName= './result/fake.png' 37 38 frame = QWidget(self) 39 40 self.canvas = Canvas(frame) 41 self.predict_canvas=PredictCanvas(frame) 42 tools = ToolFrame(frame, canvas=self.canvas,predict_canvas=self.predict_canvas) 43 44 layoutA = QVBoxLayout() 45 layoutA.addWidget(self.predict_canvas) 46 layoutA.addWidget(self.predict_canvas) 47 layoutA.addWidget(self.predict_canvas) 48 49 50 hbox = QHBoxLayout(frame) 51 52 hbox.addWidget(self.canvas) 53 54 55 hbox.addWidget(tools) 56 57#------------------該当箇所--------------------------------------------- 58 hbox.addWidget(layoutA) 59#------------------該当箇所--------------------------------------------- 60 61 62 frame.setLayout(hbox) 63 64 return frame 65 66 def setupUI(self): 67 menubar = self.menuBar() 68 self.prevTime=0.0 69 exitAct = QAction('&Exit', self) 70 exitAct.setShortcut('Ctrl+Q') 71 exitAct.triggered.connect(self.close) 72 73 saveAct = QAction('&Save', self) 74 saveAct.setShortcut('Ctrl+S') 75 saveAct.triggered.connect(self.saveFile) 76 77 fileMenu = menubar.addMenu('&File') 78 #fileMenu.addAction(resetAct) 79 #fileMenu.addAction(openAct) 80 fileMenu.addAction(saveAct) 81 fileMenu.addAction(exitAct) 82 83 # 画像付き Pencolor selectするアクションオブジェクト作成 84 selectEraserAction = QAction( QIcon('./sample/qicon/eraser.png'),'Eraser', self) 85 selectEraserAction.triggered.connect(self.selectEraser) 86 self.toolbar = self.addToolBar('Eraser') 87 self.toolbar.addAction(selectEraserAction) 88 89 # 画像付き Pencolor selectするアクションオブジェクト作成 90 selectPenAction = QAction( QIcon('./sample/qicon/pen.png'),'Pen', self) 91 selectPenAction.triggered.connect(self.selectPen) 92 self.toolbar = self.addToolBar('Pen') 93 self.toolbar.addAction(selectPenAction) 94 95 # ツールバー作成 96 selectwidthAction = QAction(QIcon('./sample/qicon/width.png'), 'Selecwidth', self) 97 selectwidthAction.triggered.connect(self.selectWidth) 98 self.toolbar = self.addToolBar('Width') 99 self.toolbar.addAction(selectwidthAction) 100 101 102 # ツールバー作成 103 selectcolorAction = QAction(QIcon('./sample/qicon/select_color.png'), 'Selectcolor', self) 104 selectcolorAction.triggered.connect(self.selectColor) 105 self.toolbar = self.addToolBar('Width') 106 self.toolbar.addAction(selectcolorAction) 107 108 # ツールバー作成 109 backAction = QAction( QIcon('./sample/qicon/undo.png'),'Back', self) 110 backAction.setShortcut('Ctrl+Z') 111 backAction.triggered.connect(self.on_back) 112 self.toolbar = self.addToolBar('Undo') 113 self.toolbar.addAction(backAction) 114 115 # ツールバー作成 116 clearAction = QAction( QIcon('./sample/qicon/clear.png'),'Clear', self) 117 clearAction.setShortcut('Ctrl+B') 118 clearAction.triggered.connect(self.on_reset) 119 self.toolbar = self.addToolBar('Undo') 120 self.toolbar.addAction(clearAction) 121 122 def on_back(self): 123 print('PyQt5 button click') 124 self.canvas.backImage() 125 126 def on_reset(self): 127 self.canvas.resetImage() 128 129 def selectEraser(self): 130 self.canvas.setPenColor(newColor=Qt.white) 131 132 def selectPen(self): 133 self.canvas.setPenWidth(newWidth=5) 134 self.canvas.setPenColor(newColor=Qt.black) 135 136 def selectColor(self): 137 self.canvas.setPenWidth(newWidth=5) 138 newColor = QColorDialog.getColor(self.canvas.penColor()) 139 self.canvas.setPenColor(newColor) 140 141 def selectWidth(self): 142 newWidth, ok = QInputDialog.getInt( 143 self, "select", 144 "select pen width: ", self.canvas.penWidth(), 1, 100, 1) 145 if ok: 146 self.canvas.setPenWidth(newWidth) 147 148 149 150class ToolFrame(QWidget): 151 def __init__(self, parent=None, canvas=None, predict_canvas=None): 152 super().__init__(parent) 153 self.predict = predict_canvas 154 self.canvas = canvas 155 #self.tester=Tester() 156 self.initUI() 157 self.predict_res_filename="./result/fake.png" 158 self.fileName='./test/test/aa.png' 159 160 def initUI(self): 161 button_predict = QPushButton('predict', self) 162 button_predict.clicked.connect(self.on_run) 163 164 button_apply = QPushButton('Apply', self) 165 button_apply.clicked.connect(self.on_apply) 166 167 vbox = QVBoxLayout(self) 168 vbox.addStretch(1) 169 vbox.addWidget(button_predict) 170 vbox.addWidget(button_apply) 171 172 @pyqtSlot() 173 def on_run(self): 174 #canvasのimageをセーブ 175 self.canvas.saveImage() 176 #上記のセーブしたimageをpredict_canvasに出力 177 #self.tester.test_main() 178 179 self.predict.PredictImage() 180 181 182class Canvas(QWidget): 183 def __init__(self, parent = None): 184 super(Canvas, self).__init__(parent) 185 186 self.myPenWidth = 2 187 self.myPenColor = Qt.black 188 self.image = QImage() 189 self.check = False 190 self.check_write = False 191 self.back = deque(maxlen = 10) 192 self.image = QImage(255, 255, QImage.Format_RGB32) 193 self.image.fill(qRgb(255, 255, 255)) 194 self.filename='./test/test/aa.png' 195 self.predict_res_filename='./result/fake.png' 196 197 def mousePressEvent(self, event): 198 if event.button() == Qt.LeftButton: 199 self.check_write = True 200 self.back.append(self.resizeImage(self.image, self.image.size())) 201 self.lastPos = event.pos() 202 self.check = True 203 204 def mouseMoveEvent(self, event): 205 if event.buttons() and Qt.LeftButton and self.check: 206 self.drawLine(event.pos()) 207 208 def mouseReleaseEvent(self, event): 209 if event.button() == Qt.LeftButton and self.check: 210 self.drawLine(event.pos()) 211 self.check = False 212 self.check_write = False 213 214 def drawLine(self, endPos): 215 painter = QPainter(self.image) 216 painter.setPen( 217 QPen(self.myPenColor, self.myPenWidth, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin) 218 ) 219 painter.drawLine(self.lastPos, endPos) 220 self.update() 221 self.lastPos = QPoint(endPos) 222 223 def paintEvent(self, event): 224 painter = QPainter(self) 225 rect = event.rect() 226 painter.drawImage(rect, self.image, rect) 227 228 def resizeImage(self, image, newSize): 229 changeImage = QImage(newSize, QImage.Format_RGB32) 230 changeImage.fill(qRgb(255, 255, 255)) 231 painter = QPainter(changeImage) 232 painter.drawImage(QPoint(0, 0), image) 233 return changeImage 234 235class PredictCanvas(QWidget): 236 def __init__(self, parent = None): 237 super(PredictCanvas, self).__init__(parent) 238 239 self.image_predict = QImage() 240 self.image_predict = QImage(255, 255, QImage.Format_RGB32) 241 self.image_predict.fill(qRgb(255*0.9, 255*0.9, 255*0.9)) 242 self.predict_res_filename='./result/fake.png' 243 244 def PredictImage(self): 245 image = QImage() 246 if not image.load('./result/fake.png'): 247 248 return False 249 250 self.image_predict = image 251 print("pre_canvas") 252 self.update() 253 return True 254 255 def paintEvent(self, event): 256 painter = QPainter(self) 257 rect = event.rect() 258 painter.drawImage(rect, self.image_predict, rect) 259 260def main(): 261 app = QApplication(sys.argv) 262 ex = MainWindow() 263 ex.setWindowTitle('Paint Tools') 264 ex.setGeometry(50, 50, 1500, 1000) 265 ex.show() 266 sys.exit(app.exec_()) 267 268if __name__ == '__main__': 269 main() 270
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/06/05 09:08
2020/06/05 09:20
2020/06/05 09:42 編集
2020/06/05 13:03 編集