本文代码来自zetcode.com
First programs in PyQt5
#!usr/bin/python3
"""
In this example, we create a simple window in PyQt5
"""
import sys
from PyQt5.QtWidgets import QApplication, QWidget
if __name__ == '__main__':
app = QApplication(sys.argv)
w = QWidget()
w.resize(250, 150)
w.move(300, 300)
w.setWindowTitle('Simple')
w.show()
sys.exit(app.exec_())
上述代码在屏幕上显示一个小窗口
最基本的窗口部件都在PyQt5.QtWidgets模块中,每一个PyQt5应用都应该创建一个应用对象,sys.argv参数是命令行中的参数列表。
w.resize(250, 150)
resize()方法调整窗口部件的大小,250px宽 150px高
w.move(300, 300)
move()方法将窗口部件放于屏幕(300, 300)坐标
w.setWindowTitle('Simple')
setWindowTitle()方法用于对窗口部件命名,显示在titlebar上
w.show()
show()方法将窗口部件展示在屏幕上,窗口部件先在内存中创建之后再显示在屏幕上
sys.exit(app.exec_())
最终,我们进入应用的主循环,sys.exit()确保程序退出完全,exec_()方法之所以有下划线是因为,exec是Python的关键词
An application Icon
应用程序的图标一般展示在窗口的左上角,在某些系统中图标可能不显示,解决方法详见Stackoverflow
#!/usr/bin/python3
"""This example shows an icon in the titlebar of the window"""
import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QIcon
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 300, 220)
self.setWindowTitle('Icon')
self.setWindowIcon(QIcon('cloud.png'))
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
setGeometry():结合了resize()和move()方法
1.前两个参数定位窗口在屏幕中的位置
2.后两个参数设定窗口的宽和高
Showing a tooltip
#!/usr/bin/python3
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):
QToolTip.setFont(QFont('SansSerif', 10))
self.setToolTip('This is a <b> QWidget</b> widget')
btn = QPushButton('Button', self)
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_())
sizeHint()给出按钮的建议尺寸
Closing a window
创建按钮点击退出
import sys
"""This program creates a quit button.
When we press the button, the application terminates"""
import sys
from PyQt5.QtWidgets import QWidget, QPushButton, QApplication
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
qbtn = QPushButton('Quit', self) # 创建按钮并添加标签'Quit'
qbtn.clicked.connect(QApplication.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_())
以下为上述代码的输出:
Message Box
退出提示
#!/usr/bin/python3
"""This program shows a confirmation message
box when we click on the close button of the application window."""
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) # 最后一个参数No是默认按钮
if reply == QMessageBox.Yes:
event.accept()
else:
event.ignore()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
Centering window on the screen
将窗口设置于桌面中心
#!/usr/bin/python3
"""This program centers a window on the screen"""
import sys
from PyQt5.QtWidgets import QWidget, QApplication, QDesktopWidget
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() # 将控件的宽高赋给qr
cp = QDesktopWidget().availableGeometry().center() # 根据分辨率得到屏幕中心
qr.moveCenter(cp) #将qr移动到屏幕中心cp
self.move(qr.topLeft()) # 将我们的应用程序左上角移动到qr的左上角
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
QDesktopWidget类提供了用户屏幕尺寸等桌面信息