PyQt5的学习之路(二)

时间:2022-04-06 08:33:47

图标

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

import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QIcon

class Myform(QWidget):
def __init__(self):
super(Myform, self).__init__()

self.initUI()

def initUI(self):
# 前两个参数定位了窗口x轴,y轴的位置,效果等同于self.move(300, 300)
# 后两个参数确定了它的尺寸,效果等同于self.resize(300, 200)
self.setGeometry(300, 300, 300, 220)

self.setWindowTitle('Icon')

self.setWindowIcon(QIcon('/home/linux/1.jpg'))

self.show()

if __name__ == '__main__':
app = QApplication(sys.argv)
w = Myform()
sys.exit(app.exec_())

tooltips

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

import sys
from PyQt5.QtWidgets import QWidget, QApplication, QToolTip, QPushButton
from PyQt5.QtGui import QFont

class Myform(QWidget):
def __init__(self):
super(Myform, self).__init__()

self.initUI()


def initUI(self):
QToolTip.setFont(QFont('SansSerif', 20))

self.setToolTip('This is a <b>QWidget</b> widget') #设置窗口的提示内容

btn = QPushButton('Button', self) # 按钮组件
btn.setToolTip('This is a <b>QPushButton</b> widget') # 设置按钮的提示内容
btn.resize(btn.sizeHint()) # 设置组件为推荐大小

btn.move(0,0) # 从内容开始移位

self.setGeometry(300, 300, 300, 300)
self.setWindowTitle('tips')
self.show()

if __name__ == '__main__':
app = QApplication(sys.argv)
w = Myform()
sys.exit(app.exec_())

关闭按钮

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

import sys
from PyQt5.QtWidgets import QWidget, QApplication, QPushButton
from PyQt5.QtCore import QCoreApplication

class Myform(QWidget):
def __init__(self):
super(Myform, self).__init__()

self.initUI()

def initUI(self):
qbtn = QPushButton('Quit', self)
# 信号&槽机制: clicked是信号,quit是槽(事件),QCoreApplication.instance()是事件对象
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)
w = Myform()
sys.exit(app.exec_())

提示框

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

import sys
from PyQt5.QtWidgets import QWidget, QApplication, QMessageBox

class Myform(QWidget):
def __init__(self):
super(QWidget, self).__init__()
self.initUI()

def initUI(self):
self.setGeometry(200, 200, 300, 300)
self.setWindowTitle('message box')
self.show()
# closeEvent的重新实现
def closeEvent(self, e):
# 参数从左到右: self, 提示窗口标题, 提示信息, 两个按钮, 默认选中
reply = QMessageBox.question(self, 'Message', 'Are you sure to quit?', QMessageBox.Yes | QMessageBox.No, QMessageBox.No)

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

if __name__ == '__main__':
app = QApplication(sys.argv)
w = Myform()
sys.exit(app.exec_())

窗口居中

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

import sys
from PyQt5.QtWidgets import QWidget, QApplication, QDesktopWidget

class Myform(QWidget):
def __init__(self):
super(Myform, self).__init__()
self.initUI()

def initUI(self):
self.resize(300, 300)
self.center()
self.setWindowTitle('居中对齐')

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)
w = Myform()
sys.exit(app.exec_())