使用pycharm中qtdsinger、pyuic设计一个绘画板用来识别鼠标写的数字完整代码
时间: 2024-03-06 10:48:16 浏览: 77
好的,以下是一个完整的代码示例,包括UI文件和Python代码:
UI文件(mypaint.ui):
```xml
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>400</height>
</rect>
</property>
<property name="windowTitle">
<string>绘画板</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>40</x>
<y>40</y>
<width>320</width>
<height>320</height>
</rect>
</property>
<property name="text">
<string/>
</property>
</widget>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>160</x>
<y>370</y>
<width>80</width>
<height>25</height>
</rect>
</property>
<property name="text">
<string>清除</string>
</property>
</widget>
</widget>
</widget>
<resources/>
<connections/>
</ui>
```
Python代码(main.py):
```python
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtGui import QPixmap, QPainter, QPen
from PyQt5.QtCore import Qt
import sys
import mypaint
class MyPaint(QMainWindow, mypaint.Ui_MainWindow):
def __init__(self, parent=None):
super(MyPaint, self).__init__(parent)
self.setupUi(self)
self.label.setPixmap(QPixmap(self.label.size()))
self.pushButton.clicked.connect(self.clear_canvas)
self.label.mousePressEvent = self.record_mouse_track
self.label.mouseMoveEvent = self.record_mouse_track
def record_mouse_track(self, event):
x, y = event.x(), event.y()
painter = QPainter(self.label.pixmap())
painter.setPen(QPen(Qt.black, 10, Qt.SolidLine))
painter.drawPoint(x, y)
painter.end()
self.update()
def clear_canvas(self):
pixmap = QPixmap(self.label.size())
pixmap.fill(Qt.white)
self.label.setPixmap(pixmap)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MyPaint()
window.show()
sys.exit(app.exec_())
```
在PyCharm中打开main.py文件,运行应用程序,开始使用绘画板识别鼠标写的数字!
阅读全文