https://blog.csdn.net/sqy941013/article/details/80548593
这个教程的第二章配置QtDesigner那里讲得不清楚,可以看这个网址:
https://blog.csdn.net/qq_42823043/article/details/88817034
spyder 3.3.4 requires pyqtwebengine<5.13, which is not installed.
https://zmister.com/archives/477.html
https://zmister.com/archives/165.html
https://blog.csdn.net/qq_38161040/article/details/86605798
https://blog.csdn.net/jia666666/article/details/81874045
https://blog.csdn.net/qq_38161040/article/details/87365818
代码:
Dialog.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
https://blog.csdn.net/brook_/article/details/80141808
https://blog.csdn.net/pursuit_zhangyu/article/details/82917276
https://blog.csdn.net/Szh19960807/article/details/80149248
代码:
self.pushButton.setStyleSheet('QPushButton{background-image:url(22.png)}')
https://www.cnblogs.com/demo-deng/p/9990090.html
self.left_mini.setStyleSheet("QPushButton{background:#6DDF6D;border-radius:5px;}QPushButton:hover{background:green;}")
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wN9lN6NT-1591288335356)(data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==)]
在这里,按钮默认为淡绿色,鼠标悬浮时为深绿色,hover就是悬浮的情况。
self.pushButton.setStyleSheet(“QPushButton{background:transparent;}”)
https://blog.csdn.net/Yuyh131/article/details/85164461
https://blog.csdn.net/jiuzuidongpo/article/details/45485127
设置labe的字体居中:
self.label.setAlignment(Qt.AlignCenter)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0LfZOg38-1591288335358)(data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==)]
此居中为上下左右居中
https://blog.csdn.net/pursuit_zhangyu/article/details/83211436
一行代码:self.button.setCursor(QCursor(Qt.PointingHandCursor))
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setupUi(self)
# 上面这两行代码就是调用Ui_MainWindow()类的界面,好好看着
self.pushButton1.clicked.connect(self.pressed_pushButton1)
def pressed_pushButton1(self):
# app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui2() # 这个Ui2是我另外的一个子窗口py文件中的类
ui.setupUi(Dialog)
Dialog.show()
Dialog.exec_()
# sys.exit(app.exec_())
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-81ZaX8bh-1591288335362)(data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==)]
class Ui2_window(QtWidgets.QDialog, Ui2):
def __init__(self):
super(Ui2_window, self).__init__() # 此处参考:https://blog.csdn.net/EXECUTER_/article/details/78877159
self.setupUi(self)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
# Dialog = QtWidgets.QDialog()
Dialog = Ui2_window()
# Dialog.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KKlUiACu-1591288335364)(data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==)]
Dialog.setWindowFlags(Qt.WindowCloseButtonHint | Qt.WindowMinMaxButtonsHint) # 设置最大小化及关闭按钮
QTableView:
https://www.cnblogs.com/ygzhaof/p/10076308.html
QTableView
https://www.zhaokeli.com/article/7986.html
QTableView设置单元格大小同图片大小改变
https://bbs.csdn.net/topics/391821557
https://www.cnblogs.com/XJT2018/p/10175881.html
1、关闭全部窗口(主窗口+所有的子窗口)
在逻辑界面中写入
sys.exit(0)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ArS2qhMf-1591288335365)(data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==)]
2、关闭子窗口(其他窗口不关闭)
self.close()
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-p6L8Yofu-1591288335367)(data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==)]
https://blog.csdn.net/jia666666/article/details/81504595
讲解清晰明白、通俗易懂
'''
简单的画板4.0
功能:
将按住鼠标后移动的轨迹保留在窗体上
并解决二次作画时与上次痕迹连续的问题
作者:PyLearn
博客: http://www.cnblogs.com/PyLearn/
最后修改日期: 2017/10/18
'''
import sys
from try_model_1pic import load_model_labels, load_image, show_results
import pylab
from PyQt5.QtWidgets import (QApplication, QWidget)
from PyQt5.QtGui import (QPainter, QPen, QPixmap)
from PyQt5.QtCore import Qt
WINDOWS_WIDTH = 400
WINDOWS_HEIGHT = 300
class Example(QWidget):
def __init__(self):
super(Example, self).__init__()
#resize设置宽高,move设置位置
self.resize(WINDOWS_WIDTH, WINDOWS_HEIGHT)
self.move(100, 100)
self.setWindowTitle("简单的画板4.0")
# 创建一个QPixmap来保存画的图片
self.map = QPixmap(WINDOWS_WIDTH, WINDOWS_HEIGHT)
self.map.fill(Qt.white) # 设置背景为白色
#setMouseTracking设置为False,否则不按下鼠标时也会跟踪鼠标事件
self.setMouseTracking(False)
'''
要想将按住鼠标后移动的轨迹保留在窗体上
需要一个列表来保存所有移动过的点
'''
self.pos_xy = []
def paintEvent(self, event):
painter = QPainter()
painter2 = QPainter()
painter.begin(self) # painter用来画画时在屏幕展出
painter2.begin(self.map) # painter2用来保存画图结果到本地
pen = QPen(Qt.black, 2, Qt.SolidLine)
painter.setPen(pen)
painter2.setPen(pen)
'''
首先判断pos_xy列表中是不是至少有两个点了
然后将pos_xy中第一个点赋值给point_start
利用中间变量pos_tmp遍历整个pos_xy列表
point_end = pos_tmp
判断point_end是否是断点,如果是
point_start赋值为断点
continue
判断point_start是否是断点,如果是
point_start赋值为point_end
continue
画point_start到point_end之间的线
point_start = point_end
这样,不断地将相邻两个点之间画线,就能留下鼠标移动轨迹了
'''
if len(self.pos_xy) > 1:
point_start = self.pos_xy[0]
for pos_tmp in self.pos_xy:
point_end = pos_tmp
if point_end == (-1, -1):
point_start = (-1, -1)
continue
if point_start == (-1, -1):
point_start = point_end
continue
painter.drawLine(point_start[0], point_start[1], point_end[0], point_end[1])
painter2.drawLine(point_start[0], point_start[1], point_end[0], point_end[1])
point_start = point_end
painter.end()
painter2.end()
def mouseMoveEvent(self, event):
'''
按住鼠标移动事件:将当前点添加到pos_xy列表中
调用update()函数在这里相当于调用paintEvent()函数
每次update()时,之前调用的paintEvent()留下的痕迹都会清空
'''
#中间变量pos_tmp提取当前点
pos_tmp = (event.pos().x(), event.pos().y())
#pos_tmp添加到self.pos_xy中
self.pos_xy.append(pos_tmp)
self.update()
def mouseReleaseEvent(self, event):
'''
重写鼠标按住后松开的事件
在每次松开后向pos_xy列表中添加一个断点(-1, -1)
然后在绘画时判断一下是不是断点就行了
是断点的话就跳过去,不与之前的连续
'''
pos_test = (-1, -1)
self.pos_xy.append(pos_test)
self.update()
self.map.save("./temp.png", "PNG")
if __name__ == "__main__":
app = QApplication(sys.argv)
pyqt_learn = Example()
pyqt_learn.show()
app.exec_()
我想要加载一个深度学习模型来分类我上面保存的图片,就直接在def mouseReleaseEvent(self, event)函数最后面加上预处理图片与模型预测:
def mouseReleaseEvent(self, event):
'''
重写鼠标按住后松开的事件
在每次松开后向pos_xy列表中添加一个断点(-1, -1)
然后在绘画时判断一下是不是断点就行了
是断点的话就跳过去,不与之前的连续
'''
pos_test = (-1, -1)
self.pos_xy.append(pos_test)
self.update()
self.map.save("./temp.png", "PNG")
image = load_image("./temp.png")
# print("image:", image) # 画画时可以加载生成的图片
results = self.model.predict(image)
# print("results:", results)
properbilities, types = show_results(self.labels, results)
加载模型我在初始化函数里完成了def init(self):
# 加载模型,类别字典
self.model, self.labels = load_model_labels()
在初始化函数def init(self):加上:
pushButton1 = QPushButton('清除', self)
pushButton1.setToolTip('清楚你的画的垃圾')
pushButton1.resize(pushButton1.sizeHint())
pushButton1.move(30, 20)
pushButton1.clicked.connect(pressed_pushButton1)
并在你的窗口类中加上它的触发函数pressed_pushButton1:
def pressed_pushButton1(self):
print("fsdfsfjioj")
完善触发函数pressed_pushButton1:
def pressed_pushButton1(self):
# 清楚保存的点集合
self.pos_xy = []
# 清除画板QWigets,通过:painter.begin(self)。
painter = QPainter()
painter.begin(self) # painter用来画画时在屏幕展出
painter.end()
# 通过重新构造来清除保存临时笔画的pixmap
self.map = QPixmap(WINDOWS_WIDTH, WINDOWS_HEIGHT)
self.map.fill(Qt.white) # 设置画板背景为白色
self.map.save(saved_temp_image, "PNG")
# 非常重要,更新
self.update()
print("清除图片")
在初始化函数def init(self)加上三个label用于放置概率与类别:
self.label_1 = QLabel(self)
self.label_1.move(WINDOWS_WIDTH*0.15, WINDOWS_HEIGHT*0.85)
self.label_1.setStyleSheet("QLabel{color:rgb(0,0,0,255);font-size:20px;border:2px solid black;"
"font-weight:bold;font-family:Arial;border-radius:50px}"
"QLabel:hover{color:rgb(255,0,0,255)}")
self.label_1.setAlignment(Qt.AlignTop) # 水平方向靠左对齐
self.label_1.setText("aaaaaa\nbbbb")
self.label_1.adjustSize() # 自动根据文字内容多少调节窗口大小
self.label_2 = QLabel(self)
self.label_2.move(WINDOWS_WIDTH * 0.45, WINDOWS_HEIGHT * 0.85)
self.label_2.setStyleSheet("QLabel{color:rgb(0,0,0,255);font-size:20px;border:2px solid black;"
"font-weight:bold;font-family:Arial;border-radius:50px}"
"QLabel:hover{color:rgb(255,0,0,255)}")
self.label_2.setAlignment(Qt.AlignTop) # 水平方向靠左对齐
self.label_2.setText("aaaaaa\nbbbb")
self.label_2.adjustSize() # 自动根据文字内容多少调节窗口大小
self.label_3 = QLabel(self)
self.label_3.move(WINDOWS_WIDTH * 0.75, WINDOWS_HEIGHT * 0.85)
self.label_3.setStyleSheet("QLabel{color:rgb(0,0,0,255);font-size:20px;border:2px solid black;"
"font-weight:bold;font-family:Arial;border-radius:50px}"
"QLabel:hover{color:rgb(255,0,0,255)}")
self.label_3.setAlignment(Qt.AlignTop) # 水平方向靠左对齐
self.label_3.setText("aaaaaa\nbbbb")
self.label_3.adjustSize() # 自动根据文字内容多少调节窗口大小
在def mouseReleaseEvent(self, event)函数加载预测结果并放入labels
properbilities, types = show_results(self.labels, results)
self.label_1.setText("{0:.1f}%\n{1}".format(properbilities[0], types[0]))
self.label_2.setText("{0:.1f}%\n{1}".format(properbilities[1], types[1]))
self.label_3.setText("{0:.1f}%\n{1}".format(properbilities[2], types[2]))
self.label_1.adjustSize()
self.label_2.adjustSize()
self.label_3.adjustSize()
在用pyUIC转化QTdesigner生成的ui文件为python代码时,遇到错误:
D:\Pro2\Anaconda3\envs\tensorflow\python.exe -m PyQt5.uic.pyuic mainUI.ui -o mainUI.py
Error: No such file or directory: “mainUI.ui”
转换下环境就可以了,可能你安装的pyuic不在当前环境下。
NameError: name ‘QIcon’ is not defined
只要 from PyQt5.QtGui import QIcon 就可以了
Python&PyQt5报错:AttributeError: module ‘PyQt5.QtGui’ has no attribute ‘QMainWindow’
https://blog.csdn.net/weixin_42137589/article/details/81489363
在import中将QtGui改成QtWidgets即可
第一个pycharm+pyqt5程序 (解决.ui文件生成的.py文件运行不出现界面问题)
https://blog.csdn.net/yogima/article/details/80589255
import sys
from first import Ui_MainWindow # 这里的first是.ui文件生成的.py文件名
from PyQt5 import QtWidgets
# 这个类继承界面UI类
class mywindow(QtWidgets.QWidget, Ui_MainWindow):
def __init__(self):
super(mywindow, self).__init__()
self.setupUi(self)
#调用show
if __name__=="__main__":
app=QtWidgets.QApplication(sys.argv)
myshow=mywindow()
myshow.show()
sys.exit(app.exec_())
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kkXLnDLU-1591288335368)(data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==)]
self.pushButton1.clicked.connect(self.pressed_pushButton1())
TypeError: argument 1 has unexpected type 'NoneType'
把pushButton1后面的括号去掉就可以了
!!!当你不知道你的控件需要import什么库时,ctrl+鼠标点击该控件,查看库文件,从文件开头的module就可知该控件所需import的库,如果没有module字样,就直接import 你的这个控件。
一些常见的错误参考网址:
1.NameError: name ‘QApplication’ is not defined
from PyQt5.QtWidgets import QApplication
2.NameError: name ‘QLabel’ is not defined
from PyQt5.QtWidgets import *
3.NameError: name ‘QDialog’ is not defined
from PyQt5.QtWidgets import QInputDialog, QLineEdit, QDialog
4.AttributeError: ‘Form’ object has no attribute ‘connect’
In PyQt5 you need to use the connect() and emit() methods of the bound signal directly, e.g. instead of:
self.emit(pyqtSignal(‘add_post(QString)’), top_post)
self.connect(self.get_thread, pyqtSignal(“add_post(QString)”), self.add_post)
self.connect(self.get_thread, pyqtSignal(“finished()”), self.done)
use:
self.add_post.emit(top_post)
self.get_thread.add_post.connect(self.add_post)
self.get_thread.finished.connect(self.done)
https://stackoverflow.com/questions/41848769/pyqt5-object-has-no-attribute-connect
5.NameError: name ‘unicode’ is not defined
Python 3 renamed the unicode type to str, the old str type has been replaced by bytes.