做事从来不坚持的我又开始学习PyQt了。。。。。。

时间:2023-03-09 02:30:19
做事从来不坚持的我又开始学习PyQt了。。。。。。

链接附上,不再更新:PyQt5图形界面编程

第一部分

第一个程序

 # -*- coding: utf-8 -*-

 import sys
from PyQt5.QtWidgets import QApplication, QWidget, QToolTip, QPushButton
from PyQt5.QtGui import QIcon, QFont class Example(QWidget): def __init__(self):
super(Example, self).__init__()
self.initUI() def initUI(self):
'''
在这里我们有两个部件可以显示提示
一个是窗口本身,另一个是按钮
''' #设置提示字体和字号
QToolTip.setFont(QFont('微软雅黑', 10)) #给组件本身设置一个提示
self.setToolTip('this is a widget') #添加一个按钮
btn = QPushButton('Button', self)
#并添加提示
btn.setToolTip('this is a pushbtn')
#设定按钮大小和位置,使用推荐大小
btn.resize(btn.sizeHint())
btn.move(50, 50) #设置窗口的位置和宽高
self.setGeometry(300, 300, 300, 300)
#设置窗口标题
self.setWindowTitle('icon')
#设置窗口图标
self.setWindowIcon(QIcon('dog.jpg')) self.show() if __name__ == '__main__': app = QApplication(sys.argv)
example = Example()
sys.exit(app.exec_())

如何优雅地退出窗口

 # -*- coding: utf-8 -*-

 import sys
from PyQt5.QtWidgets import QApplication, QWidget,QPushButton
from PyQt5.QtCore import QCoreApplication class Example(QWidget): def __init__(self):
super(Example, self).__init__()
self.initUI() def initUI(self): #添加一个按钮
btn = QPushButton('Quit', self)
btn.clicked.connect(QCoreApplication.instance().quit)
#设定按钮大小和位置,使用推荐大小
btn.resize(btn.sizeHint())
btn.move(50, 50) #设置窗口的位置和宽高
self.setGeometry(300, 300, 300, 300)
#设置窗口标题
self.setWindowTitle('quit') self.show() if __name__ == '__main__': app = QApplication(sys.argv)
example = Example()
sys.exit(app.exec_())

PyQt5中的事件处理系统是由信号槽机制(signals and slots)实现的。如果我们点击了这个按钮,就会发出“clicked”这个信号。QtCore.QCoreApplication这个东西包含了程序的主循环,它处理和分派所有的事件,而instance()方法返回的是目前的实例(insatnce)。注意到QtCore.QCoreApplication随着QtGui.QApplication的创建而创建,而由于我们这里用connect()函数将“clicked”事件和可以终止应用的quit()函数联系(connect)在了一起,所以点击按钮,应用终止。这种交流在两个对象之间完成:发送者和接受者,其中发送者是按钮,接受者是应用本身。【这里只要大家对connect方法有个感性的认识就可以了】

消息框

 # -*- coding: utf-8 -*-

 import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMessageBox class Example(QWidget): def __init__(self):
super(Example, self).__init__()
self.initUI() def initUI(self): #设置窗口的位置和宽高
self.setGeometry(300, 300, 300, 300)
#设置窗口标题
self.setWindowTitle('Message Box')
self.show() def closeEvent(self, event):
'''
这里我们设定显示一个有两个选项(yes & no)的消息框(message box)
QtGui.QMessageBox.question()方法的第二个参数是出现在标题栏的标题
第三个参数是消息框显示的对话内容,第四个参数是出现在消息框的按钮的组合【用或( | )连接】
最后一个参数是默认按钮,即消息框刚跳出来的时候按enter键就可以执行的按钮
这里我们将函数的返回值存储在了reply这个变量中
'''
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)
example = Example()
sys.exit(app.exec_())

居中显示

 # -*- coding: utf-8 -*-

 import sys
from PyQt5.QtWidgets import QApplication, QWidget, QDesktopWidget class Example(QWidget): def __init__(self):
super(Example, self).__init__()
self.initUI() def initUI(self): #设置窗口的位置和宽高
self.resize(500, 500)
#设置窗口标题
self.setWindowTitle('center widget')
self.center()
self.show() def center(self):
# 得到主窗口的矩形框架qr
qr = self.frameGeometry()
# 我们调用这些方法来得到屏幕分辨率,并最终得到屏幕中间点的坐标cp
cp = QDesktopWidget().availableGeometry().center()
# 这里我们将矩形框架移至屏幕正*,大小不变
qr.moveCenter(cp)
# 最后我们将应用窗口移至矩形框架的左上角点,这样应用窗口就位于屏幕的*了【注意部件的move都是左上角移动到某点】
self.move(qr.topLeft()) if __name__ == '__main__': app = QApplication(sys.argv)
example = Example()
sys.exit(app.exec_())

第二部分

状态栏(statusbar)

 # -*- coding: utf-8 -*-

 import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow class Example(QMainWindow): def __init__(self):
super(Example, self).__init__()
self.initUI() def initUI(self):
self.statusBar().showMessage('Ready')
#设置窗口的位置和宽高
self.setGeometry(300, 300, 250, 150)
#设置窗口标题
self.setWindowTitle('statusbar')
self.show() if __name__ == '__main__': app = QApplication(sys.argv)
example = Example()
sys.exit(app.exec_())

菜单栏(menubar)

 # -*- coding: utf-8 -*-

 import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, qApp
from PyQt5.QtGui import QIcon class Example(QMainWindow): def __init__(self):
super(Example, self).__init__()
self.initUI() def initUI(self):
# 创建一个选项有自己的图标和名字
exitAct = QAction(QIcon('dog.jpg'), '&Exit', self)
# 设置对应的一个快捷键
exitAct.setShortcut('CTRL+Q')
# 当鼠标悬停在状态栏的提示信息
exitAct.setStatusTip('Exit Application')
# 当我们选择了这个选项时,一个触发信号(triggered signal)被发出了
# 这个信号和qApp部件的quit()方法相联系(connect),所以信号发出后,程序终止
exitAct.triggered.connect(qApp.quit) statusbar = self.statusBar() menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(exitAct) self.setGeometry(300,300,300,300)
self.setWindowTitle('simple menu')
self.show() if __name__ == '__main__': app = QApplication(sys.argv)
example = Example()
sys.exit(app.exec_())

工具栏

# -*- coding: utf-8 -*-

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, qApp
from PyQt5.QtGui import QIcon class Example(QMainWindow): def __init__(self):
super(Example, self).__init__()
self.initUI() def initUI(self):
# 创建一个选项有自己的图标和名字
exitAct = QAction(QIcon('dog.jpg'), '&Exit', self)
# 设置对应的一个快捷键
exitAct.setShortcut('CTRL+Q')
# 当我们选择了这个选项时,一个触发信号(triggered signal)被发出了
# 这个信号和qApp部件的quit()方法相联系(connect),所以信号发出后,程序终止
exitAct.triggered.connect(qApp.quit) self.toolbar = self.addToolBar('Exit')
self.toolbar.addAction(exitAct) self.setGeometry(300,300,300,300)
self.setWindowTitle('simple menu')
self.show() if __name__ == '__main__': app = QApplication(sys.argv)
example = Example()
sys.exit(app.exec_())