Commit 5f87c7d4 authored by SCtom's avatar SCtom
parents 21977ef4 3b0fbec1
from PyQt5.QtCore import Qt, QDir, QFile, QPoint, QRect, QSize
from PyQt5.QtWidgets import QWidget, QPushButton, QLineEdit, QLabel, QGridLayout
from PyQt5.QtGui import QImage, QImageWriter, QPainter, QPen, qRgb, QPixmap
class Button(QWidget):
def __init__(self,parent=None, label="Button", on_click=None):
super(Button,self).__init__(parent)
button = QPushButton(label, self)
button.move(220,0)
button.clicked.connect(on_click)
grid = QGridLayout()
grid.addWidget(button,1,0,1,2)
self.setLayout(grid)
class ComboWidget(QWidget):
def __init__(self, parent=None,
text_on_label="Label",
text_on_button="button",
on_click=None, readOnly=False):
super(ComboWidget,self).__init__(parent)
button = QPushButton(text_on_button, self)
lbl = QLabel(self)
lbl.setText(text_on_label)
self.qle = QLineEdit(self)
self.qle.setReadOnly(readOnly)
self.qle.setAlignment(Qt.AlignCenter)
button.clicked.connect(on_click)
grid = QGridLayout()
grid.addWidget(lbl,1,0)
grid.addWidget(self.qle,1,1)
grid.addWidget(button,1,2)
self.setLayout(grid)
def text(self):
return self.qle.text()
def set_text(self, text=""):
self.qle.setText(text)
class Board(QWidget):
def __init__(self,parent=None):
super(Board,self).__init__(parent)
self.setAttribute(Qt.WA_StaticContents)
self.penWidth = 50
self.writing = False
self.penColor = Qt.black
self.image = QImage()
self.lastPoint = QPoint()
self.setFixedSize(512,512)
def saveImage(self,label):
import os
visibleImage = self.image
self.resizeImage(visibleImage,self.size())
if QDir('Image').exists() == False:
QDir().mkdir('Image')
label = 'Image/'+label
if QDir(label).exists() == False:
QDir().mkdir(label)
cnt = 0
maxf = 0
for file in os.listdir(label):
if file.endswith('.bmp'):
cnt += 1
tmpN = int(file[:-4:])
maxf = max(maxf,tmpN)
visibleImage.save('tmp.bmp')
pixmap = QPixmap('tmp.bmp').scaled(8,8)
pixmap.save(label+'/'+str(max(cnt,maxf)+1)+'.bmp')
QFile.remove('tmp.bmp')
def paintEvent(self, event):
painter = QPainter(self)
dirtyRect = event.rect()
painter.drawImage(dirtyRect, self.image, dirtyRect)
def clearImage(self):
self.image.fill(qRgb(255,255,255))
self.update()
def mousePressEvent(self,event):
if event.button() == Qt.LeftButton:
self.lastPoint = event.pos()
self.writing = True
def mouseMoveEvent(self,event):
if (event.buttons() & Qt.LeftButton) and self.writing:
self.drawLineTo(event.pos())
def mouseReleaseEvent(self,event):
if event.button() == Qt.LeftButton and self.writing:
self.drawLineTo(event.pos())
self.writing = False
def resizeEvent(self,event):
self.resizeImage(self.image,self.size())
self.update()
super(Board,self).resizeEvent(event)
def drawLineTo(self, endPoint):
painter = QPainter(self.image)
painter.setPen(QPen(self.penColor, self.penWidth, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))
painter.drawLine(self.lastPoint, endPoint)
self.writing = True
rad = self.penWidth/2 + 2
self.update(QRect(self.lastPoint, endPoint).normalized().adjusted(-rad, -rad, +rad, +rad))
self.lastPoint = QPoint(endPoint)
def resizeImage(self,image, newSize):
if image.size() == newSize:
return
newImage = QImage(newSize, QImage.Format_RGB32)
newImage.fill(qRgb(255,255,255))
painter = QPainter(newImage)
painter.drawImage(QPoint(0,0), image)
self.image = newImage
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QAction, QApplication, QWidget,QGridLayout)
#from board import Board
#from clearButton import ClearButton
#from trainWidget import TrainWidget
#from testWidget import TestWidget
from ubuwidgets import (Button, ComboWidget, Board)
class MainWindow(QWidget):
def __init__(self,parent=None):
super(MainWindow, self).__init__(parent)
self.scribbleArea = Board()
self.clearButton = Button(self, "Clear Image", self.on_clear)
self.saveCombo = ComboWidget(self, "Label: ", "Save", self.on_save)
self.testCombo = ComboWidget(self, "Result: ", "Test", self.on_test, True)
grid = QGridLayout()
grid.addWidget(self.scribbleArea,1,0)
grid.addWidget(self.clearButton,2,0)
grid.addWidget(self.saveCombo,3,0)
grid.addWidget(self.testCombo,4,0)
self.setLayout(grid)
self.setWindowTitle("number ubu")
self.resize(512, 650)
def on_clear(self):
self.scribbleArea.clearImage()
def on_save(self):
print('on_save')
self.scribbleArea.saveImage(self.saveCombo.text())
self.scribbleArea.clearImage()
def on_test(self):
print("testing...")
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment