python PyQt5中文教程☞【第二节】PyQt5基本功能(创建窗口、应用程序图标、显示提示语、通过按钮关闭窗口、消息框(关闭窗口确认框)、窗口显示在屏幕中间【居中显示】)
引用文章:http://code.py40.com/pyqt5/
文章目录
- 简单的例子:创建一个小窗口
- 应用程序的图标
- 显示提示语
- 通过按钮关闭窗口
- 消息框(关闭窗口确认框)
- 窗口显示在屏幕的中间【居中显示】
简单的例子:创建一个小窗口
PyQt5是一种高级的语言,下面只有几行代码就能显示一个小窗口。底层已经实现了窗口的基本功能。
# -*- coding: utf-8 -*- """ @File : test4.py @Time : 2020/4/13 16:57 @Author : Dontla @Email : sxana@qq.com @Software: PyCharm """ # !/usr/bin/python3 # -*- coding: utf-8 -*-import sys# 这里我们提供必要的引用。基本控件位于pyqt5.qtwidgets模块中。 from PyQt5.QtWidgets import QApplication, QWidgetif __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()方法确保应用程序干净的退出# app的exec_()方法有下划线。因为exec()执行是一个Python关键词,所以用exec_()代替sys.exit(app.exec_())结果:
应用程序的图标
应用程序图标是一个小的图像,通常在标题栏的左上角显示。在下面的例子中我们将介绍如何做pyqt5的图标。同时我们也将介绍一些新方法。
#!/usr/bin/python3 # -*- coding: utf-8 -*-""" py40 PyQt5 tutorialThis 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 QIconclass 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('timg.jpg'))# 显示窗口self.show()if __name__ == '__main__':# 创建应用程序和对象app = QApplication(sys.argv)ex = Example()sys.exit(app.exec_())软件图标的ICON还是用带透明通道的png图为好
结果:
Python编程语言支持程序和面向对象编程风格。Pyqt5使用OOP编程。
面向对象编程有三个重要的方面:类、变量和方法。这里我们创建一个新的类为Examle。Example继承自QWidget类。
显示提示语
在下面的例子中我们显示一个提示语
#!/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并为他设置一个tooltipbtn = 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_())结果:
通过按钮关闭窗口
关闭一个窗口可以点击标题栏上的X。在下面的例子中,我们将展示我们如何通过编程来关闭窗口。
#!/usr/bin/python3 # -*- coding: utf-8 -*-""" Py40 PyQt5 tutorialThis 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 QCoreApplicationclass Example(QWidget):def __init__(self):super().__init__()self.initUI()def initUI(self):self.setGeometry(300, 300, 250, 150)self.setWindowTitle('Quit button')qbtn = QPushButton('Quit、Quit、Quit\n退出、退出、退出', self)# Dontla 20200414 将按钮绑定退出?qbtn.clicked.connect(QCoreApplication.instance().quit)qbtn.resize(qbtn.sizeHint())qbtn.move(50, 50)self.show()if __name__ == '__main__':app = QApplication(sys.argv)ex = Example()sys.exit(app.exec_())结果:
消息框(关闭窗口确认框)
默认情况下,如果我们单击x按钮窗口就关门了。有时我们想修改这个默认的行为。例如我们在编辑器中修改了一个文件,当关闭他的时候,我们显示一个消息框确认。
#!/usr/bin/python3 # -*- coding: utf-8 -*-""" ZetCode PyQt5 tutorialThis 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, QApplicationclass 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):# Dontla 20200414 最后一个参数是默认高亮的选择项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()事件处理程序。
我们显示一个消息框,两个按钮:“是”和“不是”。第一个字符串出现在titlebar。第二个字符串消息对话框中显示的文本。第三个参数指定按钮的组合出现在对话框中。最后一个参数是默认按钮,这个是默认的按钮焦点。
我们处理返回值,如果单击Yes按钮,关闭小部件并终止应用程序。否则我们忽略关闭事件。
窗口显示在屏幕的中间【居中显示】
下面的脚本显示了如何在屏幕中心显示窗口。
#!/usr/bin/python3 # -*- coding: utf-8 -*-""" Py40 PyQt5 tutorialThis 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, QApplicationclass Example(QWidget):def __init__(self):super().__init__()self.initUI()def initUI(self):# Dontla 20200414 不像setGeometry()函数那样指定位置和长宽,这个只指定长宽self.resize(250, 150)self.center()self.setWindowTitle('Center')self.show()# 控制窗口显示在屏幕中心的方法def center(self):# 获得窗口qr = self.frameGeometry()# print(qr) # PyQt5.QtCore.QRect(0, 0, 249, 149)# 获得屏幕中心点cp = QDesktopWidget().availableGeometry().center()# print(cp) # PyQt5.QtCore.QPoint(682, 363)# 显示到屏幕中心qr.moveCenter(cp)# Dontla 20200414 不懂这句跟上面那句啥区别?意思?# self.move(qr.topLeft()) # origin# print(qr.topLeft()) # PyQt5.QtCore.QPoint(558, 289)if __name__ == '__main__':app = QApplication(sys.argv)ex = Example()sys.exit(app.exec_())结果:
QtGui,QDesktopWidget类提供了用户的桌面信息,包括屏幕大小。
总结
以上是生活随笔为你收集整理的python PyQt5中文教程☞【第二节】PyQt5基本功能(创建窗口、应用程序图标、显示提示语、通过按钮关闭窗口、消息框(关闭窗口确认框)、窗口显示在屏幕中间【居中显示】)的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: python PyQt5中文教程☞【第一
- 下一篇: python PyQt5 sizeHin