PyQt5案例汇总(完整版)

起步

PyQt5是一套绑定Qt5的应用程序框架。他在Python 2.x和3.x中都是可用的。该教程使用的是Python3.x。

Qt库是一套最有用的GUI库。

PyQt5是作为一套Python模块实现的。他已经超过620个类和6000个函数与方法。他是一个运行在所有主流操作系统上的多平台组件,包括Unix,Windows和Mac OS。

说明

下面小编就给大家提供一些简单的pyqt5的案例,如有需要拿走不谢!!!

本文转载from:PyQt5-Chinese-tutorial

菜单栏和工具栏

01窗口居中


# 导入需要的包和模块
import sys
from PyQt5.QtWidgets import QWidget
from PyQt5.QtWidgets import QDesktopWidget
# QDesktopWidget这个库提供了用户的桌面信息,包括屏幕的大小
from PyQt5.QtWidgets import QApplication

# 创建一个类
class Ex(QWidget):
 def __init__(self): super().__init__() self.initUI()
 def initUI(self): self.resize(250,150) self.center() # 这个方法调用我们下面写的,实现对话框居中的方法
 self.setWindowTitle('chuangkou要居中')
 self.show()
 def center(self): qr = self.frameGeometry() # 得到了主窗口大小
 print('qr:',qr) cp = QDesktopWidget().availableGeometry().center() # 获取显示器的分辨率,然后得到中间点的位置
 print('cp:',cp) qr.moveCenter(cp) # 然后把自己的窗口的中心点放到qr的中心点
 self.move(qr.topLeft())
app = QApplication(sys.argv)
demo1 = Ex()
sys.exit(app.exec_())

02 状态栏


# 导入需要的包和模块
import sys
# from PyQt5.QtWidgets import QWidget
# from PyQt5.QtWidgets import QDesktopWidget
# QDesktopWidget这个库提供了用户的桌面信息,包括屏幕的大小
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QMainWindow

class Ex(QMainWindow):
 def __init__(self): super().__init__() self.initUI()
 def initUI(self): # 状态栏是由这个创建的
 self.statusBar().showMessage('准备')
 # 调用QtGui.QMainWindow 类的 statusBar()方法
 #3 创建状态栏.第一次调用创建一个状态栏,返回一个状态栏对象.
 #3 showMessage()方法在状态栏上显示一条信息
 self.setGeometry(300,300,250,150) self.setWindowTitle('标题还是要取的')
 #显示
 self.show()
app = QApplication(sys.argv)
demo1 = Ex()
sys.exit(app.exec_())

03菜单栏


import sys
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QAction
from PyQt5.QtWidgets import qApp
from PyQt5.QtGui import QIcon

class Ex(QMainWindow):

 def __init__(self): super(Ex, self).__init__() self.initUI()
 def initUI(self): exitAct = QAction(QIcon("exit.png"),'&Exit',self) print(exitAct) exitAct.setShortcut("ctrl+q") exitAct.setStatusTip('tuichu应用')
 exitAct.triggered.connect(qApp.quit)
 self.statusBar()
 menubar = self.menuBar() fileMenu = menubar.addMenu('&File') fileMenu.addAction(exitAct)
 self.setGeometry(300,300,399,200) self.setWindowTitle('决赛你电脑的')
 self.show()
app = QApplication(sys.argv)
demo1 = Ex()
sys.exit(app.exec_())

04子菜单


import sys
from PyQt5.QtWidgets import QMainWindow, QAction, QMenu, QApplication

class Example(QMainWindow):

 def __init__(self): super().__init__() self.initUI()

 def initUI(self):
 menubar = self.menuBar() fileMenu = menubar.addMenu('File') impMenu = QMenu('Import', self) impAct = QAction('Import mail', self) impMenu.addAction(impAct)
 newAct = QAction('New', self)
 fileMenu.addAction(newAct) fileMenu.addMenu(impMenu)
 self.setGeometry(300, 300, 300, 200) self.setWindowTitle('Submenu') self.show()

app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

05 勾选菜单


import sys
from PyQt5.QtWidgets import QMainWindow, QAction, QApplication

class Example(QMainWindow):

 def __init__(self): super().__init__()
 self.initUI()

 def initUI(self):
 self.statusbar = self.statusBar() self.statusbar.showMessage('Ready')
 menubar = self.menuBar() viewMenu = menubar.addMenu('View')
 # 本例创建了一个行为菜单。这个行为/动作能切换状态栏显示或者隐藏。
 viewStatAct = QAction('View statusbar', self, checkable=True) viewStatAct.setStatusTip('View statusbar')        # 用checkable选项创建一个能选中的菜单。
 viewStatAct.setChecked(True)        # 默认设置为选中状态
 viewStatAct.triggered.connect(self.toggleMenu)
 viewMenu.addAction(viewStatAct) # 依据选中状态切换状态栏的显示与否。
 self.setGeometry(300, 300, 300, 200) self.setWindowTitle('Check menu') self.show()
 def toggleMenu(self, state):
 if state: self.statusbar.show() else: self.statusbar.hide()
app = QApplication(sys.argv)
demo1 = Example()
sys.exit(app.exec_())

06 右键菜单


import sys
from PyQt5.QtWidgets import QMainWindow, qApp, QMenu, QApplication

class Example(QMainWindow):

 def __init__(self): super().__init__()
 self.initUI()
 def initUI(self): self.setGeometry(300, 300, 300, 200) self.setWindowTitle('Context menu') self.show()
 def contextMenuEvent(self, event): cmenu = QMenu(self)
 newAct = cmenu.addAction("New") print(newAct) opnAct = cmenu.addAction("Open") print(opnAct) quitAct = cmenu.addAction("Quit") action = cmenu.exec_(self.mapToGlobal(event.pos()))
 if action == quitAct: qApp.quit() elif action == opnAct: print('打开就打开')
 elif action == newAct: print('新建就新建')

app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

07工具栏


# 菜单栏包含了所有的命令,工具栏就是常用的命令的集合。

import sys
from PyQt5.QtWidgets import QMainWindow, QAction, qApp, QApplication
from PyQt5.QtGui import QIcon

class Example(QMainWindow):

 def __init__(self): super().__init__()
 self.initUI()

 def initUI(self):
 exitAct = QAction(QIcon('logo.png'), 'Exit', self) exitAct.setShortcut('Ctrl+Q') exitAct.triggered.connect(qApp.quit) # 和上面的菜单栏差不多,这里使用了一个行为对象,
 # 这个对象绑定了一个标签,一个图标和一个快捷键。
 # 这些行为被触发的时候,会调用QtGui.QMainWindow的quit方法退出应用。
 self.toolbar = self.addToolBar('Exit') self.toolbar.addAction(exitAct)
 self.setGeometry(300, 300, 300, 200) self.setWindowTitle('Toolbar') self.show()

app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
# 上面的例子中,我们创建了一个工具栏这个工具栏只有一个退出应用的动作

08主窗口(啥都有的呢)


# 本模块的功能:<>
import sys
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QTextEdit
from PyQt5.QtWidgets import QAction
from PyQt5.QtWidgets import QApplication
from PyQt5.QtGui import QIcon

class Example(QMainWindow):

 def __init__(self): super().__init__() self.initUI()

 def initUI(self):
 textEdit = QTextEdit() self.setCentralWidget(textEdit)
 exitAct = QAction(QIcon('logo.png'), '退退退', self)
 exitAct.setShortcut('Ctrl+Q') exitAct.setStatusTip('退出应用')
 exitAct.triggered.connect(self.close)
 self.statusBar()
 menubar = self.menuBar() fileMenu = menubar.addMenu('文件')
 fileMenu.addAction(exitAct)
 toolbar = self.addToolBar('退出')
 toolbar.addAction(exitAct)
 self.setGeometry(300, 300, 350, 250) self.setWindowTitle('代码编辑工具')
 self.show()

app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

布局管理

09绝对定位的应用


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

class Example(QWidget):

 def __init__(self): super().__init__()
 self.initUI()

 def initUI(self):
 lbl1 = QLabel('Zetcode', self) lbl1.move(15, 10)
 lbl2 = QLabel('tutorials', self) lbl2.move(35, 40)
 lbl3 = QLabel('for programmers', self) lbl3.move(55, 70)
 self.setGeometry(300, 300, 250, 150) self.setWindowTitle('Absolute') self.show()

app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

# 绝对定位其实说白了就是使用相对于原点的像素来进行计算

10 盒子布局


import sys
from PyQt5.QtWidgets import (QWidget, QPushButton,
 QHBoxLayout, QVBoxLayout, QApplication)

class Example(QWidget):

 def __init__(self): super().__init__()
 self.initUI()

 def initUI(self):
 okButton = QPushButton("OK") cancelButton = QPushButton("Cancel")
 hbox = QHBoxLayout() hbox.addStretch(1) hbox.addWidget(okButton) hbox.addWidget(cancelButton)
 vbox = QVBoxLayout() vbox.addStretch(1) vbox.addLayout(hbox)
 self.setLayout(vbox)
 self.setGeometry(300, 300, 300, 150) self.setWindowTitle('Buttons') self.show()

app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

11 栅格布局(表格)


import sys
from PyQt5.QtWidgets import (QWidget, QGridLayout,
 QPushButton, QApplication)

class Example(QWidget):

 def __init__(self): super().__init__() self.initUI()

 def initUI(self):
 grid = QGridLayout() self.setLayout(grid)
 names = ['Cls', 'Bck', '', 'Close', '7', '8', '9', '/', '4', '5', '6', '*', '1', '2', '3', '-', '0', '.', '=', '+']
 positions = [(i,j) for i in range(5) for j in range(4)]
 for position, name in zip(positions, names):
 if name == '': continue button = QPushButton(name) grid.addWidget(button, *position)
 self.move(300, 150) self.setWindowTitle('Calculator') self.show()

app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

12 制作提交反馈信息的布局


import sys
from PyQt5.QtWidgets import
 (QWidget, QLabel, QLineEdit, QTextEdit, QGridLayout, QApplication)

class Example(QWidget):

 def __init__(self): super().__init__()
 self.initUI()
 def initUI(self): title = QLabel('Title') author = QLabel('Author') review = QLabel('Review')
 titleEdit = QLineEdit() authorEdit = QLineEdit() reviewEdit = QTextEdit()
 grid = QGridLayout() grid.setSpacing(10)
 grid.addWidget(title, 1, 0) grid.addWidget(titleEdit, 1, 1)
 grid.addWidget(author, 2, 0) grid.addWidget(authorEdit, 2, 1)
 grid.addWidget(review, 3, 0) grid.addWidget(reviewEdit, 3, 1, 5, 1)
 self.setLayout(grid)
 self.setGeometry(300, 300, 350, 300) self.setWindowTitle('Review') self.show()

app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

事件和信号

13 信号和槽机制


'''
事件
signals and slots 被其他人翻译成信号和槽机制,(⊙o⊙)…我这里还是不翻译好了。
所有的应用都是事件驱动的。事件大部分都是由用户的行为产生的,当然也有其他的事件产生方式,
比如网络的连接,窗口管理器或者定时器等。调用应用的exec_()方法时,应用会进入主循环,主循环会监听和分发事件。
在事件模型中,有三个角色:
事件源
事件
事件目标
事件源就是发生了状态改变的对象。事件是这个对象状态改变的内容。
事件目标是事件想作用的目标。事件源绑定事件处理函数,然后作用于事件目标身上。
PyQt5处理事件方面有个signal and slot机制。Signals and slots用于对象间的通讯。
事件触发的时候,发生一个signal,slot是用来被Python调用的
(相当于一个句柄?这个词也好恶心,就是相当于事件的绑定函数)slot只有在事件触发的时候才能调用。
'''

import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import
 (QWidget, QLCDNumber, QSlider, QVBoxLayout, QApplication)

class Example(QWidget):

 def __init__(self): super().__init__()
 self.initUI()

 def initUI(self):
 lcd = QLCDNumber(self) print("lcd:",lcd) sld = QSlider(Qt.Horizontal, self) print("sld",sld) vbox = QVBoxLayout() print(vbox) vbox.addWidget(lcd) vbox.addWidget(sld)
 self.setLayout(vbox) print(lcd.display) sld.valueChanged.connect(lcd.display)
 self.setGeometry(300, 300, 250, 150) self.setWindowTitle('信号和槽机制的')
 self.show()

app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

14 重构事件处理器


import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QWidget, QApplication

class Example(QWidget):

 def __init__(self): super().__init__()
 self.initUI()

 def initUI(self):
 self.setGeometry(300, 300, 250, 150) self.setWindowTitle('事件的手')
 self.show()

 def keyPressEvent(self, e):
 if e.key() == Qt.Key_Escape: self.close()

app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

15 事件对像


import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QWidget, QApplication, QGridLayout, QLabel

class Example(QWidget):

 def __init__(self): super().__init__()
 self.initUI()

 def initUI(self):
 grid = QGridLayout() grid.setSpacing(10)
 x = 0 y = 0
 self.text = "x: {0},  y: {1}".format(x, y)
 self.label = QLabel(self.text, self) grid.addWidget(self.label, 0, 0, Qt.AlignTop)
 self.setMouseTracking(True)
 self.setLayout(grid)
 self.setGeometry(300, 300, 350, 200) self.setWindowTitle('Event object') self.show()

 def mouseMoveEvent(self, e):
 x = e.x() y = e.y()
 text = "x: {0},  y: {1}".format(x, y) self.label.setText(text)

app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

16事件发送


import sys
from PyQt5.QtWidgets import QMainWindow, QPushButton, QApplication

class Example(QMainWindow):

 def __init__(self): super().__init__()
 self.initUI()

 def initUI(self):
 btn1 = QPushButton("按钮老大", self)
 btn1.move(30, 50)
 btn2 = QPushButton("按钮老二", self)
 btn2.move(150, 50)
 btn1.clicked.connect(self.buttonClicked) btn2.clicked.connect(self.buttonClicked)
 self.statusBar()
 self.setGeometry(300, 300, 290, 150) self.setWindowTitle('事件发送')
 self.show()

 def buttonClicked(self):
 sender = self.sender() self.statusBar().showMessage(sender.text() + '被按那儿了')

app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

17 信号发送


import sys
from PyQt5.QtCore import pyqtSignal, QObject
from PyQt5.QtWidgets import QMainWindow, QApplication

class Communicate(QObject):

 closeApp = pyqtSignal()

class Example(QMainWindow):

 def __init__(self): super().__init__()
 self.initUI()

 def initUI(self):
 self.c = Communicate() self.c.closeApp.connect(self.close)
 self.setGeometry(300, 300, 290, 150) self.setWindowTitle('Emit signal') self.show()

 def mousePressEvent(self, event):
 self.c.closeApp.emit()

if __name__ == '__main__':

 app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())```

# 对话框

### 18 对话框(能够输入文字呦呦)
```python

from PyQt5.QtWidgets import (QWidget, QPushButton, QLineEdit,
 QInputDialog, QApplication)import sys

class Example(QWidget):

 def __init__(self): super().__init__()
 self.initUI()

 def initUI(self):
 self.btn = QPushButton('Dialog', self) self.btn.move(20, 20) self.btn.clicked.connect(self.showDialog)
 self.le = QLineEdit(self) self.le.move(130, 22)
 self.setGeometry(300, 300, 290, 150) self.setWindowTitle('Input dialog') self.show()

 def showDialog(self):
 text, ok = QInputDialog.getText(self, 'Input Dialog', 'Enter your name:')
 if ok: self.le.setText(str(text))
 print(text+"哈哈")

app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

19 选取颜色(NB坏了)


# 本模块的功能:<QColorDialog提供颜色的选择>
# TODO 这个厉害,直接调用系统的颜色选择框
# TODO 强,实在是强
from PyQt5.QtWidgets import (QWidget, QPushButton, QFrame,
 QColorDialog, QApplication)from PyQt5.QtGui import QColor
import sys

class Example(QWidget):

 def __init__(self): super().__init__()
 self.initUI()

 def initUI(self):
 col = QColor(0, 0, 0)
 self.btn = QPushButton('Dialog', self) self.btn.move(20, 20)
 self.btn.clicked.connect(self.showDialog)
 self.frm = QFrame(self) self.frm.setStyleSheet("QWidget { background-color: %s }" % col.name()) self.frm.setGeometry(130, 22, 100, 100)
 self.setGeometry(300, 300, 250, 180) self.setWindowTitle('Color dialog') self.show()

 def showDialog(self):
 col = QColorDialog.getColor()
 if col.isValid(): self.frm.setStyleSheet("QWidget { background-color: %s }" % col.name())

app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

加油,都看到一半了你!!!


'''

 ┌─┐       ┌─┐ + + ┌──┘ ┴───────┘ ┴──┐++ │                 │ │       ───       │++ + + + ███████───███████ │+ │                 │+ │       ─┴─       │ │                 │ └───┐         ┌───┘ │         │ │         │   + + │         │ │         └──────────────┐ │                        │ │                        ├─┐ │                        ┌─┘ │                        │ └─┐  ┐  ┌───────┬──┐  ┌──┘  + + + + │ ─┤ ─┤       │ ─┤ ─┤ └──┴──┘       └──┴──┘  + + + +  神兽保佑
 代码无BUG!

'''

20 选择字体


from PyQt5.QtWidgets import (QWidget, QVBoxLayout, QPushButton,
 QSizePolicy, QLabel, QFontDialog, QApplication)import sys

class Example(QWidget):

 def __init__(self): super().__init__()
 self.initUI()

 def initUI(self):
 vbox = QVBoxLayout()
 btn = QPushButton('来来来', self)
 btn.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
 btn.move(20, 20)
 vbox.addWidget(btn)
 btn.clicked.connect(self.showDialog)
 self.lbl = QLabel('Knowledge only matters', self) self.lbl.move(130, 20)
 vbox.addWidget(self.lbl) self.setLayout(vbox)
 self.setGeometry(300, 300, 250, 180) self.setWindowTitle('字体目录')
 self.show()

 def showDialog(self):
 font, ok = QFontDialog.getFont() if ok: self.lbl.setFont(font) print('选择的字体是',end="")
 print(font) print(type(font))

app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

21 选择文件


from PyQt5.QtWidgets import
 (QMainWindow, QTextEdit, QAction, QFileDialog, QApplication)from PyQt5.QtGui import QIcon
import sys

class Example(QMainWindow):

 def __init__(self): super().__init__()
 self.initUI()

 def initUI(self):
 self.textEdit = QTextEdit() self.setCentralWidget(self.textEdit) self.statusBar()
 openFile = QAction(QIcon('images/open.png'), 'Open', self) openFile.setShortcut('Ctrl+O') openFile.setStatusTip('打开一个新的文件')
 openFile.triggered.connect(self.showDialog)
 menubar = self.menuBar() fileMenu = menubar.addMenu('&文件')
 fileMenu.addAction(openFile)
 self.setGeometry(300, 300, 350, 300) self.setWindowTitle('File dialog') self.show()

 def showDialog(self):
 fname = QFileDialog.getOpenFileName(self, 'Open file', '/home')
 if fname[0]: f = open(fname[0], 'r')
 with f: data = f.read() self.textEdit.setText(data) print(data)app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

控件

22 QCheckBox是啥玩意


from PyQt5.QtWidgets import QWidget, QCheckBox, QApplication
from PyQt5.QtCore import Qt
import sys

class Example(QWidget):

 def __init__(self): super().__init__()
 self.initUI()

 def initUI(self):
 cb = QCheckBox('改改改', self)
 cb.move(20, 20) cb.toggle() cb.stateChanged.connect(self.changeTitle)
 self.setGeometry(300, 300, 250, 150) self.setWindowTitle('真正的标题')
 self.show()

 def changeTitle(self, state):
 if state == Qt.Checked: self.setWindowTitle('假装有标题')
 else: self.setWindowTitle('没了')

app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

23 切换按钮


from PyQt5.QtWidgets import (QWidget, QPushButton,
 QFrame, QApplication)from PyQt5.QtGui import QColor
import sys

class Example(QWidget):

 def __init__(self): super().__init__()
 self.initUI()

 def initUI(self):
 self.col = QColor(0, 0, 0)
 redb = QPushButton('Red', self) redb.setCheckable(True) redb.move(10, 10)
 redb.clicked[bool].connect(self.setColor)
 greenb = QPushButton('Green', self) greenb.setCheckable(True) greenb.move(10, 60)
 greenb.clicked[bool].connect(self.setColor)
 blueb = QPushButton('Blue', self) blueb.setCheckable(True) blueb.move(10, 110)
 blueb.clicked[bool].connect(self.setColor)
 self.square = QFrame(self) self.square.setGeometry(150, 20, 100, 100) self.square.setStyleSheet("QWidget { background-color: %s }" % self.col.name())
 self.setGeometry(300, 300, 280, 170) self.setWindowTitle('Toggle button') self.show()

 def setColor(self, pressed):
 source = self.sender()
 if pressed: val = 255 else: val = 0
 if source.text() == "Red": self.col.setRed(val) elif source.text() == "Green": self.col.setGreen(val) else: self.col.setBlue(val)
 self.square.setStyleSheet("QFrame { background-color: %s }" % self.col.name())

if __name__ == '__main__':

 app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())```

### 24 滑块是个好东西
```python

from PyQt5.QtWidgets import (QWidget, QSlider,
 QLabel, QApplication)from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap
import sys

class Example(QWidget):

 def __init__(self): super().__init__()
 self.initUI()
 def initUI(self):
 sld = QSlider(Qt.Horizontal, self) sld.setFocusPolicy(Qt.NoFocus) sld.setGeometry(30, 40, 100, 30) sld.valueChanged[int].connect(self.changeValue)
 self.label = QLabel(self) self.label.setPixmap(QPixmap('images/logo.png')) self.label.setGeometry(160, 40, 80, 30)
 self.setGeometry(300, 300, 280, 170) self.setWindowTitle('s什么鸡儿玩意r')
 self.show()
 def changeValue(self, value):
 if value == 0: self.label.setPixmap(QPixmap('images/1.png')) elif value > 0 and value <= 30: self.label.setPixmap(QPixmap('images/2.png')) elif value > 30 and value < 80: self.label.setPixmap(QPixmap('images/3.png')) else: self.label.setPixmap(QPixmap('images/4.png'))

app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

25 进度条


from PyQt5.QtWidgets import (QWidget, QProgressBar,
 QPushButton, QApplication)from PyQt5.QtCore import QBasicTimer
import sys

class Example(QWidget):

 def __init__(self): super().__init__()
 self.initUI()

 def initUI(self):
 self.pbar = QProgressBar(self) self.pbar.setGeometry(30, 40, 200, 25)
 self.btn = QPushButton('走你', self)
 self.btn.move(40, 80) self.btn.clicked.connect(self.doAction)
 self.timer = QBasicTimer() self.step = 0
 self.setGeometry(300, 300, 280, 170) self.setWindowTitle('进度条就比较强了')
 self.show()

 def timerEvent(self, e):
 if self.step >= 200: self.timer.stop() self.btn.setText('完成吧')
 return
 self.step = self.step + 1 self.pbar.setValue(self.step)
 def doAction(self):
 if self.timer.isActive(): self.timer.stop() self.btn.setText('走走走')
 else: self.timer.start(200, self) self.btn.setText('停,呗走了')

if __name__ == '__main__':

 app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())```

### 26 日历
```python

from PyQt5.QtWidgets import (QWidget, QCalendarWidget,
 QLabel, QApplication, QVBoxLayout)from PyQt5.QtCore import QDate
import sys

class Example(QWidget):

 def __init__(self): super().__init__()
 self.initUI()

 def initUI(self):
 vbox = QVBoxLayout(self)
 cal = QCalendarWidget(self) cal.setGridVisible(True) cal.clicked[QDate].connect(self.showDate)
 vbox.addWidget(cal)
 self.lbl = QLabel(self) date = cal.selectedDate() self.lbl.setText(date.toString())
 vbox.addWidget(self.lbl)
 self.setLayout(vbox)
 self.setGeometry(300, 300, 350, 300) self.setWindowTitle('看日历,认准我')
 self.show()

 def showDate(self, date):
 self.lbl.setText(date.toString())
if __name__ == '__main__':

 app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())```

### 27 图片
```python

from PyQt5.QtWidgets import (QWidget, QHBoxLayout,
 QLabel, QApplication)from PyQt5.QtGui import QPixmap
import sys

class Example(QWidget):

 def __init__(self): super().__init__()
 self.initUI()

 def initUI(self):
 hbox = QHBoxLayout(self) pixmap = QPixmap("images/09f04")
 lbl = QLabel(self) lbl.setPixmap(pixmap)
 hbox.addWidget(lbl) self.setLayout(hbox)
 self.move(300, 200) self.setWindowTitle('Red Rock') self.show()

if __name__ == '__main__':

 app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())```

### 28行编辑
```python

import sys
from PyQt5.QtWidgets import
 (QWidget, QLabel, QLineEdit, QApplication)

class Example(QWidget):

 def __init__(self): super().__init__()
 self.initUI()
 def initUI(self): self.lbl = QLabel(self) qle = QLineEdit(self)
 qle.move(60, 100) self.lbl.move(60, 40)
 qle.textChanged[str].connect(self.onChanged)
 self.setGeometry(300, 300, 280, 170) self.setWindowTitle('QLineEdit') self.show()
 def onChanged(self, text): self.lbl.setText(text) self.lbl.adjustSize()

app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

29 QSplitter是啥玩意呢?


from PyQt5.QtWidgets import (QWidget, QHBoxLayout, QFrame,
 QSplitter, QStyleFactory, QApplication)from PyQt5.QtCore import Qt
import sys

class Example(QWidget):

 def __init__(self): super().__init__()
 self.initUI()

 def initUI(self):
 hbox = QHBoxLayout(self)
 topleft = QFrame(self) topleft.setFrameShape(QFrame.StyledPanel)
 topright = QFrame(self) topright.setFrameShape(QFrame.StyledPanel)
 bottom = QFrame(self) bottom.setFrameShape(QFrame.StyledPanel)
 splitter1 = QSplitter(Qt.Horizontal) splitter1.addWidget(topleft) splitter1.addWidget(topright)
 splitter2 = QSplitter(Qt.Vertical) splitter2.addWidget(splitter1) splitter2.addWidget(bottom)
 hbox.addWidget(splitter2) self.setLayout(hbox)
 self.setGeometry(300, 300, 400, 300) self.setWindowTitle('QSplitter') self.show()

 def onChanged(self, text):
 self.lbl.setText(text) self.lbl.adjustSize()

app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

30 下拉选框


from PyQt5.QtWidgets import (QWidget, QLabel,
 QComboBox, QApplication)import sys

class Example(QWidget):

 def __init__(self): super().__init__()
 self.initUI()

 def initUI(self):
 self.lbl = QLabel("Ubuntu", self)
 combo = QComboBox(self) combo.addItem("Ubuntu") combo.addItem("Windows") combo.addItem("centos") combo.addItem("deepin") combo.addItem("redhat") combo.addItem("debain") combo.move(50, 50) self.lbl.move(50, 150)
 combo.activated[str].connect(self.onActivated)
 self.setGeometry(300, 300, 300, 200) self.setWindowTitle('下拉选框练习 ') self.show()

 def onActivated(self, text):
 self.lbl.setText(text) self.lbl.adjustSize()

app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

拖拽

31 简单的拖放

from PyQt5.QtWidgets import (QPushButton, QWidget,
 QLineEdit, QApplication)import sys

class Button(QPushButton):

 def __init__(self, title, parent): super().__init__(title, parent)
 self.setAcceptDrops(True)

 def dragEnterEvent(self, e):
 if e.mimeData().hasFormat('text/plain'): e.accept() else: e.ignore()
 def dropEvent(self, e):
 self.setText(e.mimeData().text())

class Example(QWidget):

 def __init__(self): super().__init__()
 self.initUI()

 def initUI(self):
 edit = QLineEdit('', self) edit.setDragEnabled(True) edit.move(30, 65)
 button = Button("Button", self) button.move(190, 65)
 self.setWindowTitle('Simple drag and drop') self.setGeometry(300, 300, 300, 150)

if __name__ == '__main__':

 app = QApplication(sys.argv) ex = Example() ex.show() app.exec_()```

### 32 拖放按钮组件
```python

from PyQt5.QtWidgets import QPushButton, QWidget, QApplication
from PyQt5.QtCore import Qt, QMimeData
from PyQt5.QtGui import QDrag
import sys

class Button(QPushButton):

 def __init__(self, title, parent): super().__init__(title, parent)

 def mouseMoveEvent(self, e):
 if e.buttons() != Qt.RightButton: return
 mimeData = QMimeData()
 drag = QDrag(self) drag.setMimeData(mimeData) drag.setHotSpot(e.pos() - self.rect().topLeft())
 dropAction = drag.exec_(Qt.MoveAction)

 def mousePressEvent(self, e):
 super().mousePressEvent(e)
 if e.button() == Qt.LeftButton: print('按我嘎哈')

class Example(QWidget):

 def __init__(self): super().__init__()
 self.initUI()

 def initUI(self):
 self.setAcceptDrops(True)
 self.button = Button('来吧!!!', self)
 self.button.move(100, 65)
 self.setWindowTitle('点击还能挪')
 self.setGeometry(300, 300, 280, 150)

 def dragEnterEvent(self, e):
 e.accept()

 def dropEvent(self, e):
 position = e.pos() self.button.move(position)
 e.setDropAction(Qt.MoveAction) e.accept()

if __name__ == '__main__':

 app = QApplication(sys.argv) ex = Example() ex.show() app.exec_()"""
拖拽
在GUI里,拖放是指用户点击一个虚拟的对象,拖动,然后放置到另外一个对象上面的动作。
一般情况下,需要调用很多动作和方法,创建很多变量。
拖放能让用户很直观的操作很复杂的逻辑。
一般情况下,我们可以拖放两种东西:数据和图形界面。
把一个图像从一个应用拖放到另外一个应用上的实质是操作二进制数据。
把一个表格从Firefox上拖放到另外一个位置 的实质是操作一个图形组。
"""

绘图

33 文本的涂鸦(这个好玩哈)


import sys
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtGui import QPainter, QColor, QFont
from PyQt5.QtCore import Qt

class Example(QWidget):

 def __init__(self): super().__init__()
 self.initUI()

 def initUI(self):
 self.text = "涂鸦要涂的有灵魂"

 self.setGeometry(300, 300, 280, 170) self.setWindowTitle('绘画板')
 self.show()

 def paintEvent(self, event):
 qp = QPainter() qp.begin(self) self.drawText(event, qp,168, 34, 243) qp.end()
 # qp1 = QPainter() # qp1.begin(self) # self.drawText(event, qp1,168, 34, 23) # qp1.end()

 def drawText(self, event, qp, r,g,b):
 qp.setPen(QColor(r,g,b)) qp.setFont(QFont('微软雅黑', 15))
 qp.drawText(event.rect(), Qt.AlignCenter, self.text)

if __name__ == '__main__':

 app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())```

### 34 点的绘画
```python

from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtGui import QPainter
from PyQt5.QtCore import Qt
import sys, random

class Example(QWidget):

 def __init__(self): super().__init__()
 self.initUI()

 def initUI(self):
 self.setGeometry(300, 300, 300, 190) self.setWindowTitle('一大堆点点儿')
 self.show()

 def paintEvent(self, e):
 qp = QPainter() qp.begin(self) self.drawPoints(qp) qp.end()

 def drawPoints(self, qp):
 qp.setPen(Qt.red) size = self.size()
 for i in range(1000): x = random.randint(1, size.width()-1) y = random.randint(1, size.height()-1) qp.drawPoint(x, y)

if __name__ == '__main__':

 app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())```

### 35 颜色
```python

from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtGui import QPainter, QColor, QBrush
import sys

class Example(QWidget):

 def __init__(self): super().__init__()
 self.initUI()

 def initUI(self):
 self.setGeometry(300, 300, 350, 100) self.setWindowTitle('Colours') self.show()

 def paintEvent(self, e):
 qp = QPainter() qp.begin(self) self.drawRectangles(qp) qp.end()

 def drawRectangles(self, qp):
 col = QColor(0, 0, 0) col.setNamedColor('#d4d4d4') qp.setPen(col)
 qp.setBrush(QColor(200, 0, 0)) qp.drawRect(10, 15, 90, 60)
 qp.setBrush(QColor(255, 80, 0, 160)) qp.drawRect(130, 15, 90, 60)
 qp.setBrush(QColor(25, 0, 90, 200)) qp.drawRect(250, 15, 90, 60)

if __name__ == '__main__':

 app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())```

### 36 QPen是笔么
```python

from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtGui import QPainter, QPen
from PyQt5.QtCore import Qt
import sys

class Example(QWidget):

 def __init__(self): super().__init__()
 self.initUI()

 def initUI(self):
 self.setGeometry(300, 300, 280, 270) self.setWindowTitle('Pen styles') self.show()

 def paintEvent(self, e):
 qp = QPainter() qp.begin(self) self.drawLines(qp) qp.end()

 def drawLines(self, qp):
 pen = QPen(Qt.black, 2, Qt.SolidLine)
 qp.setPen(pen) qp.drawLine(20, 40, 250, 40)
 pen.setStyle(Qt.DashLine) qp.setPen(pen) qp.drawLine(20, 80, 250, 80)
 pen.setStyle(Qt.DashDotLine) qp.setPen(pen) qp.drawLine(20, 120, 250, 120)
 pen.setStyle(Qt.DotLine) qp.setPen(pen) qp.drawLine(20, 160, 250, 160)
 pen.setStyle(Qt.DashDotDotLine) qp.setPen(pen) qp.drawLine(20, 200, 250, 200)
 pen.setStyle(Qt.CustomDashLine) pen.setDashPattern([1, 4, 5, 4]) qp.setPen(pen) qp.drawLine(20, 240, 250, 240)

if __name__ == '__main__':

 app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())```

### 37 QBrush是啥?
```python

from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtGui import QPainter, QBrush
from PyQt5.QtCore import Qt
import sys

class Example(QWidget):

 def __init__(self): super().__init__()
 self.initUI()

 def initUI(self):
 self.setGeometry(300, 300, 355, 280) self.setWindowTitle('Brushes') self.show()

 def paintEvent(self, e):
 qp = QPainter() qp.begin(self) self.drawBrushes(qp) qp.end()

 def drawBrushes(self, qp):
 brush = QBrush(Qt.SolidPattern) qp.setBrush(brush) qp.drawRect(10, 15, 90, 60)
 brush.setStyle(Qt.Dense1Pattern) qp.setBrush(brush) qp.drawRect(130, 15, 90, 60)
 brush.setStyle(Qt.Dense2Pattern) qp.setBrush(brush) qp.drawRect(250, 15, 90, 60)
 brush.setStyle(Qt.DiagCrossPattern) qp.setBrush(brush) qp.drawRect(10, 105, 90, 60)
 brush.setStyle(Qt.Dense5Pattern) qp.setBrush(brush) qp.drawRect(130, 105, 90, 60)
 brush.setStyle(Qt.Dense6Pattern) qp.setBrush(brush) qp.drawRect(250, 105, 90, 60)
 brush.setStyle(Qt.HorPattern) qp.setBrush(brush) qp.drawRect(10, 195, 90, 60)
 brush.setStyle(Qt.VerPattern) qp.setBrush(brush) qp.drawRect(130, 195, 90, 60)
 brush.setStyle(Qt.BDiagPattern) qp.setBrush(brush) qp.drawRect(250, 195, 90, 60)

if __name__ == '__main__':

 app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())```

### 38 贝赛尔曲线(这个学过PS的都知道)
```python

from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtGui import QPainter, QPainterPath
from PyQt5.QtCore import Qt
import sys

class Example(QWidget):

 def __init__(self): super().__init__()
 self.initUI()
 def initUI(self): # 用QPainterPath路径创建贝塞尔曲线。
 # 使用cubicTo()方法生成,分别需要三个点:起始点,控制点和终止点。
 self.setGeometry(300, 300, 380, 250) self.setWindowTitle('绘制贝塞尔曲线')
 self.show()
 def paintEvent(self, e): qp = QPainter() qp.begin(self) qp.setRenderHint(QPainter.Antialiasing) self.drawBezierCurve(qp) qp.end()
 def drawBezierCurve(self, qp): path = QPainterPath() path.moveTo(30, 30) path.cubicTo(30, 30, 350, 30, 200, 150)
 qp.drawPath(path)

if __name__ == '__main__':
 app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())

39自定义组件


from PyQt5.QtWidgets import (QWidget, QSlider, QApplication,
 QHBoxLayout, QVBoxLayout)from PyQt5.QtCore import QObject, Qt, pyqtSignal
from PyQt5.QtGui import QPainter, QFont, QColor, QPen
import sys

class Communicate(QObject):

 updateBW = pyqtSignal(int)

class BurningWidget(QWidget):

 def __init__(self): super().__init__()
 self.initUI()

 def initUI(self):
 self.setMinimumSize(1, 30) self.value = 75 self.num = [75, 150, 225, 300, 375, 450, 525, 600, 675]

 def setValue(self, value):
 self.value = value

 def paintEvent(self, e):
 qp = QPainter() qp.begin(self) self.drawWidget(qp) qp.end()

 def drawWidget(self, qp):
 MAX_CAPACITY = 700 OVER_CAPACITY = 750
 font = QFont('Serif', 7, QFont.Light) qp.setFont(font)
 size = self.size() w = size.width() h = size.height()
 step = int(round(w / 10))

 till = int(((w / OVER_CAPACITY) * self.value)) full = int(((w / OVER_CAPACITY) * MAX_CAPACITY))
 if self.value >= MAX_CAPACITY:
 qp.setPen(QColor(255, 255, 255)) qp.setBrush(QColor(255, 255, 184)) qp.drawRect(0, 0, full, h) qp.setPen(QColor(255, 175, 175)) qp.setBrush(QColor(255, 175, 175)) qp.drawRect(full, 0, till-full, h)
 else:
 qp.setPen(QColor(255, 255, 255)) qp.setBrush(QColor(255, 255, 184)) qp.drawRect(0, 0, till, h)

 pen = QPen(QColor(20, 20, 20), 1, Qt.SolidLine)
 qp.setPen(pen) qp.setBrush(Qt.NoBrush) qp.drawRect(0, 0, w-1, h-1)
 j = 0
 for i in range(step, 10*step, step):
 qp.drawLine(i, 0, i, 5) metrics = qp.fontMetrics() fw = metrics.width(str(self.num[j])) qp.drawText(i-fw/2, h/2, str(self.num[j])) j = j + 1

class Example(QWidget):

 def __init__(self): super().__init__()
 self.initUI()

 def initUI(self):
 OVER_CAPACITY = 750
 sld = QSlider(Qt.Horizontal, self) sld.setFocusPolicy(Qt.NoFocus) sld.setRange(1, OVER_CAPACITY) sld.setValue(75) sld.setGeometry(30, 40, 150, 30)
 self.c = Communicate() self.wid = BurningWidget() self.c.updateBW[int].connect(self.wid.setValue)
 sld.valueChanged[int].connect(self.changeValue) hbox = QHBoxLayout() hbox.addWidget(self.wid) vbox = QVBoxLayout() vbox.addStretch(1) vbox.addLayout(hbox) self.setLayout(vbox)
 self.setGeometry(300, 300, 390, 210) self.setWindowTitle('Burning widget') self.show()

 def changeValue(self, value):
 self.c.updateBW.emit(value) self.wid.repaint()

if __name__ == '__main__':

 app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())```

终于翻到底部了,能看到这里,给你自己一个奖励吧!!!
```python

'''

 ┌─┐       ┌─┐ + + ┌──┘ ┴───────┘ ┴──┐++ │                 │ │       ───       │++ + + + ███████───███████ │+ │                 │+ │       ─┴─       │ │                 │ └───┐         ┌───┘ │         │ │         │   + + │         │ │         └──────────────┐ │                        │ │                        ├─┐ │                        ┌─┘ │                        │ └─┐  ┐  ┌───────┬──┐  ┌──┘  + + + + │ ─┤ ─┤       │ ─┤ ─┤ └──┴──┘       └──┴──┘  + + + +  神兽保佑
 代码无BUG!

'''
本作品采用《CC 协议》,转载必须注明作者和本文链接
秋叶夏风
victorfengming
讨论数量: 1

要是能贴一些效果图就更好了

2年前 评论
laiyuqi77 2年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!