python图形开发GUI库pyqt5的基本使用方法详解


Posted in Python onFebruary 14, 2020

一:安装PyQt5

pip install pyqt5

如果你的系统没有安装pip请阅读我们的另一篇文章 windows下python安装pip方法详解

二:PyQt5简单使用

#!/usr/bin/python3
# -*- coding: utf-8 -*-
 
"""
Py40.com PyQt5 tutorial 
 
In this example, we create a simple
window in PyQt5.
 
author: Jan Bodnar
website: py40.com 
last edited: January 2015
"""
 
import sys
 
#这里我们提供必要的引用。基本控件位于pyqt5.qtwidgets模块中。
from PyQt5.QtWidgets import QApplication, QWidget
 
 
if __name__ == '__main__':
 #每一pyqt5应用程序必须创建一个应用程序对象。sys.argv参数是一个列表,从命令行输入参数。
 app = QApplication(sys.argv)
 #QWidget部件是pyqt5所有用户界面对象的基类。他为QWidget提供默认构造函数。默认构造函数没有父类。
 w = QWidget()
 #resize()方法调整窗口的大小。这离是250px宽150px高
 w.resize(250, 150)
 #move()方法移动窗口在屏幕上的位置到x = 300,y = 300坐标。
 w.move(300, 300)
 #设置窗口的标题
 w.setWindowTitle('Simple')
 #显示在屏幕上
 w.show()
 
 #系统exit()方法确保应用程序干净的退出
 #的exec_()方法有下划线。因为执行是一个Python关键词。因此,exec_()代替
 sys.exit(app.exec_())

上面的示例代码在屏幕上显示一个小窗口。

python图形开发GUI库pyqt5的基本使用方法详解

应用程序的图标

应用程序图标是一个小的图像,通常在标题栏的左上角显示。在下面的例子中我们将介绍如何做pyqt5的图标。同时我们也将介绍一些新方法。

#!/usr/bin/python3
# -*- coding: utf-8 -*-
 
"""
py40 PyQt5 tutorial 
 
This example shows an icon
in the titlebar of the window.
 
author: Jan Bodnar
website: py40.com 
last edited: January 2015
"""
 
import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QIcon
 
 
class Example(QWidget):
 
 def __init__(self):
  super().__init__()
  
  self.initUI() #界面绘制交给InitUi方法
  
  
 def initUI(self):
  #设置窗口的位置和大小
  self.setGeometry(300, 300, 300, 220) 
  #设置窗口的标题
  self.setWindowTitle('Icon')
  #设置窗口的图标,引用当前目录下的web.png图片
  self.setWindowIcon(QIcon('web.png'))  
  
  #显示窗口
  self.show()
  
  
if __name__ == '__main__':
 #创建应用程序和对象
 app = QApplication(sys.argv)
 ex = Example()
 sys.exit(app.exec_())

前面的例子是在程序风格。Python编程语言支持程序和面向对象编程风格。Pyqt5使用OOP编程。

class Example(QWidget):
 
 def __init__(self):
  super().__init__()
  ...

面向对象编程有三个重要的方面:类、变量和方法。这里我们创建一个新的类为Examle。Example继承自QWidget类。

python图形开发GUI库pyqt5的基本使用方法详解

显示提示语

在下面的例子中我们显示一个提示语

#!/usr/bin/python3
# -*- coding: utf-8 -*-
 
"""
Py40 PyQt5 tutorial 
 
This example shows a tooltip on 
a window and a button.
 
author: Jan Bodnar
website: py40.com 
last edited: January 2015
"""
 
import sys
from PyQt5.QtWidgets import (QWidget, QToolTip, 
 QPushButton, QApplication)
from PyQt5.QtGui import QFont 
 
 
class Example(QWidget):
 
 def __init__(self):
  super().__init__()
  
  self.initUI()
  
  
 def initUI(self):
  #这种静态的方法设置一个用于显示工具提示的字体。我们使用10px滑体字体。
  QToolTip.setFont(QFont('SansSerif', 10))
  
  #创建一个提示,我们称之为settooltip()方法。我们可以使用丰富的文本格式
  self.setToolTip('This is a <b>QWidget</b> widget')
  
  #创建一个PushButton并为他设置一个tooltip
  btn = QPushButton('Button', self)
  btn.setToolTip('This is a <b>QPushButton</b> widget')
  
  #btn.sizeHint()显示默认尺寸
  btn.resize(btn.sizeHint())
  
  #移动窗口的位置
  btn.move(50, 50)  
  
  self.setGeometry(300, 300, 300, 200)
  self.setWindowTitle('Tooltips') 
  self.show()
  
  
if __name__ == '__main__':
 
 app = QApplication(sys.argv)
 ex = Example()
 sys.exit(app.exec_())

运行程序,显示一个窗口

python图形开发GUI库pyqt5的基本使用方法详解

关闭窗口

关闭一个窗口可以点击标题栏上的X。在下面的例子中,我们将展示我们如何通过编程来关闭窗口。

#!/usr/bin/python3
# -*- coding: utf-8 -*-
 
"""
Py40 PyQt5 tutorial 
 
This program creates a quit
button. When we press the button,
the application terminates. 
 
author: Jan Bodnar
website: py40.com 
last edited: January 2015
"""
 
import sys
from PyQt5.QtWidgets import QWidget, QPushButton, QApplication
from PyQt5.QtCore import QCoreApplication
 
 
class Example(QWidget):
 
 def __init__(self):
  super().__init__()
  
  self.initUI()
  
  
 def initUI(self):    
  
  qbtn = QPushButton('Quit', self)
  qbtn.clicked.connect(QCoreApplication.instance().quit)
  qbtn.resize(qbtn.sizeHint())
  qbtn.move(50, 50)  
  
  self.setGeometry(300, 300, 250, 150)
  self.setWindowTitle('Quit button') 
  self.show()
  
  
if __name__ == '__main__':
 
 app = QApplication(sys.argv)
 ex = Example()
 sys.exit(app.exec_())

python图形开发GUI库pyqt5的基本使用方法详解

消息框

默认情况下,如果我们单击x按钮窗口就关门了。有时我们想修改这个默认的行为。例如我们在编辑器中修改了一个文件,当关闭他的时候,我们显示一个消息框确认。

#!/usr/bin/python3
# -*- coding: utf-8 -*-
 
"""
ZetCode PyQt5 tutorial 
 
This program shows a confirmation 
message box when we click on the close
button of the application window. 
 
author: Jan Bodnar
website: zetcode.com 
last edited: January 2015
"""
 
import sys
from PyQt5.QtWidgets import QWidget, QMessageBox, QApplication
 
 
class Example(QWidget):
 
 def __init__(self):
  super().__init__()
  
  self.initUI()
  
  
 def initUI(self):    
  
  self.setGeometry(300, 300, 250, 150)  
  self.setWindowTitle('Message box') 
  self.show()
  
  
 def closeEvent(self, event):
  
  reply = QMessageBox.question(self, 'Message',
   "Are you sure to quit?", QMessageBox.Yes | 
   QMessageBox.No, QMessageBox.No)
 
  if reply == QMessageBox.Yes:
   event.accept()
  else:
   event.ignore()  
  
  
if __name__ == '__main__':
 
 app = QApplication(sys.argv)
 ex = Example()
 sys.exit(app.exec_())

我们关闭窗口的时候,触发了QCloseEvent。我们需要重写closeEvent()事件处理程序。

reply = QMessageBox.question(self, 'Message',
 "Are you sure to quit?", QMessageBox.Yes | 
 QMessageBox.No, QMessageBox.No)

我们显示一个消息框,两个按钮:“是”和“不是”。第一个字符串出现在titlebar。第二个字符串消息对话框中显示的文本。第三个参数指定按钮的组合出现在对话框中。最后一个参数是默认按钮,这个是默认的按钮焦点。

if reply == QtGui.QMessageBox.Yes:
 event.accept()
else:
 event.ignore()

我们处理返回值,如果单击Yes按钮,关闭小部件并终止应用程序。否则我们忽略关闭事件。

python图形开发GUI库pyqt5的基本使用方法详解

窗口显示在屏幕的中间

下面的脚本显示了如何在屏幕中心显示窗口。

#!/usr/bin/python3
# -*- coding: utf-8 -*-
 
"""
Py40 PyQt5 tutorial 
 
This program centers a window 
on the screen. 
 
author: Jan Bodnar
website: py40.com 
last edited: January 2015
"""
 
import sys
from PyQt5.QtWidgets import QWidget, QDesktopWidget, QApplication
 
 
class Example(QWidget):
 
 def __init__(self):
  super().__init__()
  
  self.initUI()
  
  
 def initUI(self):    
  
  self.resize(250, 150)
  self.center()
  
  self.setWindowTitle('Center') 
  self.show()
  
 
 #控制窗口显示在屏幕中心的方法 
 def center(self):
  
  #获得窗口
  qr = self.frameGeometry()
  #获得屏幕中心点
  cp = QDesktopWidget().availableGeometry().center()
  #显示到屏幕中心
  qr.moveCenter(cp)
  self.move(qr.topLeft())
  
  
if __name__ == '__main__':
 
 app = QApplication(sys.argv)
 ex = Example()
 sys.exit(app.exec_())

QtGui,QDesktopWidget类提供了用户的桌面信息,包括屏幕大小。

本篇文章只是简单示范pyqt5的基本使用方法更详细的使用方法请查看我们的另一篇文章 python图形开发GUI库pyqt5的详细使用方法及各控件的属性与方法

更多关于python图形开发GUI库pyqt5的基本使用方法请查看下面的相关链接

Python 相关文章推荐
Python利用BeautifulSoup解析Html的方法示例
Jul 30 Python
Python设计模式之MVC模式简单示例
Jan 10 Python
利用Python将每日一句定时推送至微信的实现方法
Aug 13 Python
Python计算一个点到所有点的欧式距离实现方法
Jul 04 Python
python实现连连看辅助(图像识别)
Mar 25 Python
python实现大战外星人小游戏实例代码
Dec 26 Python
Pyqt5 关于流式布局和滚动条的综合使用示例代码
Mar 24 Python
Python requests模块session代码实例
Apr 14 Python
python中查看.db文件中表格的名字及表格中的字段操作
Jul 07 Python
举例讲解Python装饰器
Dec 24 Python
为了顺利买到演唱会的票用Python制作了自动抢票的脚本
Oct 16 Python
Python+Selenium自动化环境搭建与操作基础详解
Mar 13 Python
Python 写了个新型冠状病毒疫情传播模拟程序
Feb 14 #Python
在pycharm中实现删除bookmark
Feb 14 #Python
python图形开发GUI库wxpython使用方法详解
Feb 14 #Python
解决Pycharm中恢复被exclude的项目问题(pycharm source root)
Feb 14 #Python
Python requests模块基础使用方法实例及高级应用(自动登陆,抓取网页源码)实例详解
Feb 14 #Python
Python实现名片管理系统
Feb 14 #Python
pycharm设置当前工作目录的操作(working directory)
Feb 14 #Python
You might like
smarty实例教程
2006/11/19 PHP
php debug 安装技巧
2011/04/30 PHP
PHP获取本周第一天和最后一天示例代码
2014/02/24 PHP
fromCharCode和charCodeAt 方法
2006/12/27 Javascript
如何获取select下拉框的值(option没有及有value属性)
2013/11/08 Javascript
JavaScript实现列表分页功能特效
2015/05/15 Javascript
XMLHttpRequest Level 2 使用指南
2016/08/26 Javascript
JS实现图片预加载之无序预加载功能代码
2017/05/12 Javascript
node中Express 动态设置端口的方法
2017/08/04 Javascript
jquery radio 动态控制选中失效问题的解决方法
2018/02/28 jQuery
vue中v-text / v-html使用实例代码详解
2019/04/02 Javascript
基于Vue 撸一个指令实现拖拽功能
2019/10/09 Javascript
在Vue.js中使用TypeScript的方法
2020/03/19 Javascript
原生JavaScript实现进度条
2021/02/19 Javascript
[02:56]DOTA2英雄基础教程 巨魔战将
2013/12/10 DOTA
Python二叉树定义与遍历方法实例分析
2018/05/25 Python
python 使用turtule绘制递归图形(螺旋、二叉树、谢尔宾斯基三角形)
2019/05/30 Python
react+django清除浏览器缓存的几种方法小结
2019/07/17 Python
Python字典推导式将cookie字符串转化为字典解析
2019/08/10 Python
python实现两个文件夹的同步
2019/08/29 Python
python安装gdal的两种方法
2019/10/29 Python
浅析python 定时拆分备份 nginx 日志的方法
2020/04/27 Python
使用Python实现音频双通道分离
2020/12/25 Python
德国运动营养和健身网上商店:Myprotein.de
2018/07/18 全球购物
英国电信商店:BT Shop
2019/12/17 全球购物
UNIX文件类型
2013/08/29 面试题
中学生家长评语大全
2014/04/16 职场文书
经营理念标语
2014/06/21 职场文书
校长师德师风自我剖析材料
2014/09/29 职场文书
初中生散播谣言检讨书
2014/11/17 职场文书
平遥古城导游词
2015/02/03 职场文书
2015年乡镇平安建设工作总结
2015/05/13 职场文书
Django实现翻页的示例代码
2021/05/24 Python
MySQL 全文索引使用指南
2021/05/25 MySQL
python使用opencv对图像添加噪声(高斯/椒盐/泊松/斑点)
2022/04/06 Python
win10电脑关机快捷键是哪个 win10快速关机的几种方法
2022/08/14 数码科技