分页: 1 / 4

pyqt5即兴

发表于 : 2018-09-26 19:05
Jacky286
昨天偶而搜了一下两个绑定项目pygtk,pyqt5,发现pygtk已经6年多没有更新,而pyqt5则已经成熟到在Ubuntu18.04下一条单一命令搭建环境的程度,十分惊叹,就测试了一下,拿出来共享,大虾们就见谅一下,小白们不妨尝试一下——图形编程的简单一至于斯!
一、安装pyqt5环境
sudo apt-get install python3-pyqt5
第一个小程序:
#!/usr/bin/python3
# -*- coding:utf-8 -*-

import sys
from PyQt5.QtWidgets import QLabel,QApplication

if __name__=='__main__':
app=QApplication(sys.argv)

w=QLabel()
w.setText("Hello,PYQT5!")
w.setFixedSize(402,332)
w.move(400,200)
w.show()
sys.exit(app.exec_())

保存为
hello.py
运行
python3 hello.py
注意运行python hello.py会出错,因为python2与3不匹配

第二个小程序
#!/usr/bin/python3
# -*- coding:utf-8 -*-

import sys
from PyQt5 import QtWidgets,QtCore

if __name__=='__main__':
app = QtWidgets.QApplication(sys.argv)
widget = QtWidgets.QWidget()
widget.resize(360,360)
widget.setWindowTitle("Hello,PyQt5!")
widget.show()
sys.exit(app.exec_())
保存为
hello1.py
运行
python3 hello1.py

二、结合用qt-designer设计窗体
https://www.cnblogs.com/jmlovepython/p/5699791.html
我们通过新立得或命令安装designer
安装qt5 designer:
https://blog.csdn.net/lightningqw/artic ... s/79113335
需要先安装qt5和qt工具:
sudo apt-get install qt5-default qttools5-dev-tools
然后在终端执行designer就可以打开qt designer界面。
通过这个软件设计窗体完成后,会保存为ui文件。

三、转化窗体文件为py文件
命令为
pyuic5

转换文件命令把WindowEx.ui生成WindowEx.py:
pyuic5 -o WindowEx.py WindowEx.ui

查看一下新生成的windowEx.py:

打开该文本文件,可以看到其中自动生成一个class类Ui_MainWindow,类中有一个方法setupUi()
***不同的ui,生成的类可能不同

要使用这个现成的界面文件,可以创建一个python文件,导入windowEx.py
参考代码如下:

#!/usr/bin/python3
# -*- coding:utf-8 -*-

import sys
from PyQt5 import QtWidgets #导入相应的包
from WindowEx import *

if __name__ == '__main__':

app = QtWidgets.QApplication(sys.argv)
w = QtWidgets.QWidget()

ui=Ui_MainWindow()
ui.setupUi(w)
w.show()

sys.exit( app.exec_() )

调试
$ python3 winEx.py
Traceback (most recent call last):
File "winEx.py", line 13, in <module>
ui=Ui_Form()
NameError: name 'Ui_Form' is not defined
***此处类为Ui_MainWindow故出错

$ python3 winEx.py
Traceback (most recent call last):
File "winEx.py", line 14, in <module>
ui.setupUi(w)
File "~/prj/WindowEx.py", line 42, in setupUi
MainWindow.setCentralWidget(self.centralwidget)
AttributeError: 'QWidget' object has no attribute 'setCentralWidget'
***这是由于qt designer生成的属性与qt5不匹配造成的,删除该行即可

$ python3 winEx.py
Traceback (most recent call last):
File "winEx.py", line 14, in <module>
ui.setupUi(w)
File "~/prj/WindowEx.py", line 45, in setupUi
MainWindow.setMenuBar(self.menubar)
AttributeError: 'QWidget' object has no attribute 'setMenuBar'
$ python3 winEx.py
Traceback (most recent call last):
File "winEx.py", line 14, in <module>
ui.setupUi(w)
File "~/prj/WindowEx.py", line 47, in setupUi
MainWindow.setStatusBar(self.statusbar)
AttributeError: 'QWidget' object has no attribute 'setStatusBar'
***这是由于qt designer生成的属性与qt5不匹配造成的,删除该行即可

$ python3 winEx.py
文件生成界面见附图,可以用designer设计出更复杂的界面,同样使用以上代码调用。

下面为WindowEx.py参考代码:
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'WindowEx.ui'
#
# Created by: PyQt5 UI code generator 5.10.1
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(140, 70, 184, 164))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.radioButton = QtWidgets.QRadioButton(self.verticalLayoutWidget)
self.radioButton.setObjectName("radioButton")
self.verticalLayout.addWidget(self.radioButton, 0, QtCore.Qt.AlignHCenter)
self.radioButton_2 = QtWidgets.QRadioButton(self.verticalLayoutWidget)
self.radioButton_2.setObjectName("radioButton_2")
self.verticalLayout.addWidget(self.radioButton_2, 0, QtCore.Qt.AlignHCenter)
self.radioButton_3 = QtWidgets.QRadioButton(self.verticalLayoutWidget)
self.radioButton_3.setObjectName("radioButton_3")
self.verticalLayout.addWidget(self.radioButton_3, 0, QtCore.Qt.AlignHCenter)
self.dateTimeEdit = QtWidgets.QDateTimeEdit(self.verticalLayoutWidget)
self.dateTimeEdit.setObjectName("dateTimeEdit")
self.verticalLayout.addWidget(self.dateTimeEdit)
self.buttonBox = QtWidgets.QDialogButtonBox(self.verticalLayoutWidget)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.verticalLayout.addWidget(self.buttonBox)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 28))
self.menubar.setObjectName("menubar")
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")

self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)

def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.radioButton.setText(_translate("MainWindow", "一般"))
self.radioButton_2.setText(_translate("MainWindow", "讨厌"))
self.radioButton_3.setText(_translate("MainWindow", "喜欢"))

Re: pyqt5即兴

发表于 : 2018-09-27 22:53
Jacky286
四、融入信号反馈机制
#!/usr/bin/python3
# -*- coding:utf-8 -*-

import sys
from PyQt5 import QtWidgets #导入相应的包
from WindowEx import *
from PyQt5.QtCore import QCoreApplication
def MyShow(self):
mytext = "按下[显示]按钮"
print(mytext)

if __name__ == '__main__':

app = QtWidgets.QApplication(sys.argv)
w = QtWidgets.QWidget()

ui=Ui_MainWindow()
ui.setupUi(w)
w.show()
ui.pushButton.clicked.connect(MyShow)
ui.buttonBox.clicked.connect(QCoreApplication.instance().quit)

# ui.label.setText(w, "test")
sys.exit( app.exec_() )
保存为winEx1.py
运行:
$ python3 winEx1.py
按下[显示]按钮会打印这几个字,按[ok][cancel]程序退出

五、不转换ui文件使用qtdesigner
不主动进行uic处理,而是选择pyqt5替我们进行。代码如下
#!/usr/bin/python3
# -*- coding:utf-8 -*-

import sys
from PyQt5 import QtWidgets #导入相应的包
#from WindowEx import * #没有转换py文件,此行取消
from PyQt5.QtCore import QCoreApplication
from PyQt5.uic import loadUi #重点和秘诀就在这里,大家注意看,如何直接导用ui文件

def MyShow(self):
mytext = "按下[显示]按钮"
print(mytext)

if __name__ == '__main__':

app = QtWidgets.QApplication(sys.argv)
# w = QtWidgets.QWidget()
w = loadUi('WindowEx.ui') #看到没,瞪大眼睛看
# ui=Ui_MainWindow() #没有转换py文件,此行取消
# ui.setupUi(w) #没有转换py文件,此行取消
w.show()
# ui.pushButton.clicked.connect(MyShow) #没有转换py文件,没有此行ui对象
# ui.buttonBox.clicked.connect(QCoreApplication.instance().quit) #没有转换py文件,没有此行ui对象
w.pushButton.clicked.connect(MyShow)
w.buttonBox.clicked.connect(QCoreApplication.instance().quit)

sys.exit( app.exec_() )
保存文件为
winExUI.py
运行
python3 winExUI.py
效果与四节一致!
此节参考地址请点击:https://blog.csdn.net/chlk118/article/d ... ource=copy

Re: pyqt5即兴--打包与排错

发表于 : 2018-10-02 19:52
Jacky286
六、打包
在ubuntu中安装pyinstaller打包
关于Python中的打包的方法有很多,但是感觉其中的pyinstaller最好用,只是关于他的安装和使用大部分都是在Windows下的安装和使用,这里记录一下关于ubuntu中的安装和使用
1.安装Python3:
已安装pyqt5的一定已经安装,可略过,空白系统可[ctrl]+[alt]+[T]打开终端,然后输入:
sudo apt-get install python3
2.安装pip3:
sudo apt-get install python3-pip
3.重链接到python3:
ubuntu如果Python的版本Python2版本,重链接就可以默认使用Python3了
sudo rm /usr/bin/python
sudo ln -r /usr/local/python/python-3.4.2/python /usebin/python
4.通过pip3安装pyinstaller
pip3 install pyinstaller
用这种方式下载下来的文件路径在.local/bin
[也可用包安装,到官网去下载,有个网页收录了绝大多数的Python中的包提供给大家:
https://pypi.python.org/pypi?:action=browse&c=611
下载到本地后找个地方进行解压就可以了,笔者是放在了桌面的一个文件中,便于操作:
tar -xjf xx.tar.bz2
解压完成后进入文件夹执行
Python setup.py install
按安装完后就可以使用pyinstaller进行打包了
可以把需要打包的文件或者文件夹放在PyInstaller-3.3.1(解压缩的文件)文件下然后在终端进入当前目录进行打包
python3 pyinstaller.py xx.py
]
5.打包
可以在.local/bin文件夹中新建一个文件夹,如pyqt,然后将需打包的*.py文件复制进去,再运行命令:
python3 ./pyinstaller -Fw ./pyqt/*.py --noconsole

6. 然后会生成两个文件夹和一个文件 我们需要的可执行文件就在其中的dist文件夹中
7. 打开dist–>打包文件
在这里点击打包的文件同名的文件双击运行
***如果用命令
python3 ./pyinstaller -Fw ./pyqt/*.py
生成的文件须终端运行
python3 ./pyinstaller -F ./pyqt/*.py
打包,生成的文件须复制附属的文件才行

七、Pyqt5运行pyqt4各种错误解决方法汇总
https://blog.csdn.net/rosefun96/article ... s/79440064
1.NameError: name 'QApplication' is not defined

from PyQt5.QtWidgets import QApplication
1
2.NameError: name 'QLabel' is not defined

from PyQt5.QtWidgets import *
1
3.NameError: name 'QDialog' is not defined

from PyQt5.QtWidgets import QInputDialog, QLineEdit, QDialog
1
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)
1
2
3
use:

self.add_post.emit(top_post)
self.get_thread.add_post.connect(self.add_post)
self.get_thread.finished.connect(self.done)
1
2
3
https://stackoverflow.com/questions/418 ... te-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.

Re: pyqt5即兴 pyqt5开发ubuntu1804比windows方便得多了

发表于 : 2018-10-02 20:09
Jacky286
折腾了两天想在windows7旗舰版上安装pyqt5,但是边python3.4以上的版本也没有安装成功,可能需要升级到sp1版才行因为有网友在windows7上安装成功的,但可悲的是升级到sp1版本居然不能成功,比起ubuntu下一条命令搞定开发环境,个人感觉windows似乎走得已经越来越远了!
不知网友们有木有这种感觉

Re: pyqt5即兴

发表于 : 2018-10-19 22:19
Jacky286
上一点干货,由于对API不熟,可能与python库功能重合而代码不同
有精力的朋友可以一起上https://github.com/ShiJianGang-sh/MWdock
编程交流,GPL V3许可
本页所附代码与github有tooltip编号的差异和首页显示不同

self.model.item(0).index().data()#获取listView内容***

si=self.model.item(i).index()#获取所在行的索引号***
self.ui.listView.setCurrentIndex(si)#定位索引号行***
itemc=self.model.item(i)
itemc.setData(QVariant(Qt.Checked),Qt.CheckStateRole)#改变索引行勾选态***

self.model.setData(i1, myStr)#更新某行数据

self.fileName_choose,filetype = QFileDialog.getOpenFileName(self,"选取文件", self.cwd,# 起始路径
"All Files (*);;Pthon Files (*.py);;Text Files (*.txt);;向导文件(*.xdf)")# 设置文件扩展名过滤,用双分号间隔

将listView的0行设为编辑显示行
self.model = QStandardItemModel()
item = QStandardItem("0行为输入显示行")
item.setCheckState(False)
item.setCheckable(True)
self.model.clear()
self.model.appendRow(item)
self.ui.listView.setModel(self.model)
self.ui.show()

Re: pyqt5即兴

发表于 : 2018-10-19 22:29
Jacky286
安装pyqt5就可以运行附件,ui文件不能直接上传故压缩了一下,linuxer应该都知道右键解压,一个有趣的摘录计算程序,尝试一下吧。
对于python的简洁和强大,笔者在此处女作里也算有所体会了

Re: pyqt5即兴

发表于 : 2018-10-19 23:11
Jacky286
通过勾选左边的各行,再按右边“&gt;”钮就可以进行摘录,还可以在0行输入关键字,按[搜索]自动每次勾选相关一行,同样右&gt;进行摘录,点某行,按右上角[计算]钮可以在0行显示计算结果,[加行]将结果加到所选行后,有点意思吧……
通过勾选左边的各行,再按右边“>”钮就可以进行摘录,还可以在0行输入关键字,按[搜索]自动每次勾选相关一行,同样右>进行摘录,点某行,按右上角[计算]钮可以在0行显示计算结果,[加行]将结果加到所选行后,有点意思吧……

Re: pyqt5即兴

发表于 : 2018-10-20 13:52
Jacky286
今天浏览网络,从一条国外答问冰释了pyqt5在windows7上的安装问题,原来import PyQt5时大小写不够仔细,
https://stackoverflow.com/questions/338 ... t5-win7x64
还有这一条也很有启发
https://blog.csdn.net/a19990412/article ... s/79185860

win7下运行成功

发表于 : 2018-10-20 17:54
Jacky286
今天一路顺风,在windows7上也搭建pyqt5环境成功,运行截图,这个摘录利器可以有,勾选一下很习惯吧,希望你喜欢。
勾选操作0行可以全部选中,和全部清空;通过在0行输入关键字搜索,可以逐句选中相关行;点右侧“>”钮可以将勾选部分摘录集中到另外窗口,如图中下部分所示(这是个dock窗,可以拖放停靠,但默认是最小化在右下角,需要拉大一下,目前还没解决怎么弄成默认大一点)

Re: pyqt5即兴

发表于 : 2018-10-21 19:17
Jacky286
1、增强选择摘录功能,由之前的逐步搜索自动勾选,升级为默认逐步,但提供全文件搜索相关项勾选功能,这样可以一次生成关键字的列表项,见附图有图有真相
2、“>”摘录文档功能优化

Re: pyqt5即兴

发表于 : 2018-10-21 22:01
Jacky286
之前一直在用qt designer 界面设计器所生成的ui文本文件直接编程,今天想到打包需要的界面文件是py格式而非ui格式,所以按pyuic5 -o Mindway.py Mindway.ui命令生成py文件,再修改主程,算是主流风格的pyqt5编程,有兴趣的可以对比看看,稍微有几句不同,但用起来个人感觉反而不如ui直接编程方便,大概是要多转换一下的缘故。
测试了一下,ui格式在MS windows下也可运行,但用pyinstaller打包就有问题,这也是花点时间转这一道弯的原因,等测试在windwos下打包成一个exe运行文件后再作评论吧

Re: pyqt5即兴

发表于 : 2018-10-22 17:15
Jacky286
window7下运行报错修正

Re: pyqt5即兴

发表于 : 2018-10-24 17:07
Jacky286
幸福来得太突然,居然用pyinstaller顺利打包成功,将两个文件mwdock0.32p.py 和 MindWay.py拷贝到安装Python37\scrips\mwdock文件夹下,输入命令:
pyinstaller -F -w mwdock.0.32p.py
windows下自动找相关资源,也不用输入Mindway.py界面文件,然后就在相应路径的dist文件夹下找到了
mwdock0.32p.exe文件,只是有点肥,要33MB,给个链接,有兴趣的在windows下试下运行有没有问题
https://pan.baidu.com/s/1XwmV50NBIa0I1vnaQ2IjCg

0.46版新鲜出炉,双击运行包放送

发表于 : 2018-11-01 22:44
Jacky286
https://pan.baidu.com/s/16opzT_NmBl80Nx8qf9Qd6w
现在可以在多个文本文件中搜索,在首页的文本库页中,也可以拖放排序,列表显示排序可以通过定位文件oftenfiles.set而生效;可以折行显示列表,有木有用Linux的机电工程师,或者现在很火热的注册消防工程师,对于管理、查阅一大堆规范文本好像很有用处哦,而且它对复杂的算式可以计算,对于书写工程计算书很有好处,所谓“知其然,更知其所以然”——软件起名“所以然”了吧。
将py的原代码文件也一起上传,最大的好处是存储空间小,双击运行的要40M,原文件却只要100k,要大400倍呀,而且新手也可以拿着试手学习是不?

v0.48上传

发表于 : 2018-11-02 14:29
Jacky286
https://pan.baidu.com/s/1xlPJx5yyMf3ZPQsb3BymQQ
上面是运行包链接,在Linux下解压后双击其中可执行文件运行。
v0.48较之0.46将搜索结果扩展到关键字行至后面的空行,这样对于文本的要求有整理,即不同描述点的文本间用空行分开,也就是写文章用空行分段的意思,这样搜索结果意义更加完整,就象网络通用搜索引擎一样,内容成段。
另外,搜索结果存入临时文件,不留痕迹。
但v0.46对文本要求宽松,就事论事,可能也是一种风格,适合拿来主义。
不知哪种模式更受欢迎
源代码一并贴出