python代码打包成exe文件
1. pip install pyinstaller #打包
2. 进入到需要打包的文件夹
3. pyinstaller -w --onefile xxx.py
pyqt5 的基本使用,全部上代码:
# -*- coding: utf-8 -*-
def Basic_win(): # 基本窗口实现
import sys
from PyQt5.QtWidgets import QApplication, QWidget # widget窗口小部件
app = QApplication(sys.argv)
widget = QWidget()
widget.resize(250, 250) # 设置控件尺寸
widget.setWindowTitle('simple') # 设置界面标题名字
widget.show() # 显示出来
sys.exit(app.exec_()) # sys.exit()确保干净的退出,主循环退出,exec_()
# Basic_win()
def Icon(): # 窗口图标实现
import sys
from PyQt5.QtWidgets import QApplication, QWidget # widget窗口小部件
from PyQt5.QtGui import QIcon
app = QApplication(sys.argv)
widget = QWidget()
widget.resize(250, 250) # 设置控件尺寸
widget.setWindowTitle('simple') # 设置界面标题名字
widget.setGeometry(700, 300, 250, 250)
widget.setWindowIcon(QIcon('mail.ico')) # 需要ico格式
widget.show() # 显示出来
sys.exit(app.exec_()) # sys.exit()确保干净的退出,主循环退出,exec_()
# Icon()
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(10)
self.setToolTip("This is a <b>QWidget</b> widget")
btn = QPushButton("Button", self)
btn.setToolTip("This is a <b>QPushButon</b> widget")
btn.resize(btn.sizeHint())
btn.move(50, 50)
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle("Tooltips")
self.setWindowIcon(QIcon('mail.ico')) # 需要ico格式
self.show()
# if __name__ == "__main__":
# app = QApplication(sys.argv)
# ex = Example()
# sys.exit(app.exec_())
from PyQt5.QtWidgets import QWidget, QPushButton, QApplication
from PyQt5.QtCore import QCoreApplication
# 窗口关闭
class Example(QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
qbtn = QPushButton("Quit", self) # 创建按钮
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)
# ex = Example()
# sys.exit(app.exec_())
from PyQt5.QtWidgets import QWidget, QApplication, QMessageBox
from PyQt5.QtCore import QCoreApplication
# 窗口关闭对话框
class Example(QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle("message box")
self.show()
def closeEvent(self, QCloseEvent):
reply = QMessageBox.question(self, 'message', 'are you sure to quit?', QMessageBox.Yes | QMessageBox.No,
QMessageBox.No)
if reply == QMessageBox.Yes:
QCloseEvent.accept()
else:
QCloseEvent.ignore()
# if __name__ == "__main__":
# app = QApplication(sys.argv)
# ex = Example()
# sys.exit(app.exec_())
import sys
from PyQt5.QtWidgets import QWidget, QDesktopWidget, QApplication
# 整个窗口居中
class Example(QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.resize(250, 150)
self.center()
self.setWindowTitle('Center')
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)
# ex = Example()
# sys.exit(app.exec_())
import sys
from PyQt5.QtWidgets import QWidget, QDesktopWidget, QApplication, QMainWindow
# 状态栏
class Example(QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.statusBar().showMessage('Ready') # 设置状态栏的信息.显示
self.resize(250, 150)
self.center()
self.setWindowTitle('Center')
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)
# ex = Example()
# sys.exit(app.exec_())
import sys
from PyQt5.QtWidgets import QWidget, QDesktopWidget, QApplication, QMainWindow, QAction, qApp
# 菜单栏
class Example(QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
exitAction = QAction(QIcon('mail.ico'), '&Exit', self)
exitAction.setShortcut('Ctrl+Q')
exitAction.setStatusTip('Exit application')
exitAction.triggered.connect(qApp.exit) # triggered发出信号,这个信号连接quit方法,退出
self.statusBar()
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(exitAction)
self.resize(250, 150)
self.center()
self.setWindowTitle('Center')
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)
# ex = Example()
# sys.exit(app.exec_())
import sys
from PyQt5.QtWidgets import QMainWindow, QAction, qApp, QApplication
from PyQt5.QtGui import QIcon
# 工具栏
class Example(QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
exitAction = QAction(QIcon('mail.ico'), "Exit", self)
exitAction = QAction(QIcon("exit.png"), "Exit", self)
exitAction.setShortcut("Ctrl+Q")
exitAction.triggered.connect(qApp.quit)
self.toolbar = self.addToolBar("Exit")
self.toolbar.addAction(exitAction)
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle("Toolbar")
self.setWindowIcon(QIcon("SmartMedicineKit.png"))
self.show()
# if __name__ == "__main__":
# app = QApplication(sys.argv)
# ex = Example()
# sys.exit(app.exec_())
#
import sys
from PyQt5.QtWidgets import QMainWindow, QTextEdit, QAction, QApplication
# 菜单栏+提示栏+工具栏
class Example(QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.initui()
def initui(self):
textEdit = QTextEdit()
self.setCentralWidget(textEdit) # 填充界面
exit=QAction(QIcon('mail.ico'),'exit',self)
exit.setShortcut('ctrl+o')
exitAction = QAction(QIcon('mail.ico'), 'Exit', self) # QAction是菜单栏、工具栏或自定义快捷键中可以执行的动作的抽象表示
exitAction.setShortcut('Ctrl+Q') # 设置快捷键
exitAction.setStatusTip('Exit application') # 设置提示
exitAction.triggered.connect(qApp.exit) # 点击这个动作时会发出triggered信号。这个信号连接到了QApplication的quit()方法
self.statusBar()
menubar = self.menuBar() # 创建一个菜单栏
fileMenu = menubar.addMenu('File') # 添加菜单
fileMenu.addAction(exitAction)
toolbar = self.addToolBar('exit') # 创建工具栏
toolbar.addAction(exitAction) # 工具栏上添加动作
self.setGeometry(300, 300, 300, 300)
self.setWindowTitle('main window')
self.setWindowIcon(QIcon('mail.ico'))
self.show()
#
# if __name__ == '__main__':
# app=QApplication(sys.argv) #创建一个app对象,sys.argv是从命令行传入的参数列表
# ex=Example()
# sys.exit(app.exec_())
#
import sys
from PyQt5.QtWidgets import QWidget, QLabel, QApplication, QHBoxLayout, QVBoxLayout
# 布局管理:Box layout
class Example(QWidget):
def __init__(self):
super(Example, self).__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) # 将vbox设为窗体的主布局
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle("Absolute")
self.show()
# if __name__ == "__main__":
# app = QApplication(sys.argv)
# ex = Example()
# sys.exit(app.exec_())
#
import sys
from PyQt5.QtWidgets import QWidget, QLabel, QApplication, QHBoxLayout, QVBoxLayout, QGridLayout
# 布局管理:GridLayout
class Example(QWidget):
def __init__(self):
super(Example, self).__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 positions, name in zip(positions, names):
if name == '':
continue # 继续循环
button = QPushButton(name)
grid.addWidget(button, *positions)
self.move(300, 150)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle("Absolute")
self.show()
#
# if __name__ == "__main__":
# app = QApplication(sys.argv)
# ex = Example()
# sys.exit(app.exec_())
import sys
from PyQt5.QtWidgets import (QWidget, QLabel, QLineEdit, QTextEdit, QGridLayout, QApplication)
class Example(QWidget):
def __init__(self):
super(Example, self).__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)
self.setLayout(grid)
self.setGeometry(300, 300, 350, 300)
self.setWindowTitle("Review")
self.show()
# if __name__ == "__main__":
# app = QApplication(sys.argv)
# ex = Example()
# sys.exit(app.exec_())
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QWidget, QLCDNumber, QSlider, QVBoxLayout, QApplication)
#滑动块
class Example(QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
lcd = QLCDNumber(self) # lcd的值会随着滑块的拖动而改变
sld = QSlider(Qt.Horizontal, self) # 滑动条
vbox = QVBoxLayout() # 竖直排列
vbox.addWidget(lcd)
vbox.addWidget(sld)
self.setLayout(vbox)
sld.valueChanged.connect(lcd.display) # 将滑动条的valueChanged信号连接到lcd的display插槽
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle("Signal & slott")
self.show()
#
# if __name__ == "__main__":
# app = QApplication(sys.argv)
# ex = Example()
# sys.exit(app.exec_())
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QWidget, QApplication
#按enter键后关闭程序
class Example(QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle("Event handler")
self.show()
def keyPressEvent(self, e):
if e.key() == Qt.key_Enter:#如果按下enter键就会执行xxx,这些键名都在Qt中
self.close()
#
# if __name__ == "__main__":
# app = QApplication(sys.argv)
# ex = Example()
# sys.exit(app.exec_())
#
#
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QWidget, QApplication
#按任意键或者鼠标键后关闭程序
class Example(QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle("Event handler")
self.show()
def keyPressEvent(self, e):
#按下任意键就会执行关闭操作
self.close()
def mousePressEvent(self, e):
#按下鼠标键就会执行关闭操作
self.close()
#
# if __name__ == "__main__":
# app = QApplication(sys.argv)
# ex = Example()
# sys.exit(app.exec_())
from PyQt5.QtWidgets import QMainWindow,QPushButton,QApplication
class Example(QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
btn1=QPushButton('按钮1',self)#创建一个按钮
btn1.move(30,50)#移动到相对于显示出来的窗口位置
btn2=QPushButton('按钮2',self)
btn2.move(150,50)
btn1.clicked.connect(self.buttonClicked)#击按钮1的时候连接到函数
btn2.clicked.connect(self.buttonClicked)
self.statusBar()#创建状态栏
self.setGeometry(300,300,300,300)
self.setWindowTitle('enter sender')
self.show()
def buttonClicked(self):
sender=self.sender() #通过调用sender()方法来判断当前按下的是哪个按钮
self.statusBar().showMessage(sender.text()+'was pressed')
# if __name__ == '__main__':
# app=QApplication(sys.argv)
# ex=Example()
# sys.exit(app.exec_())
#
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(Example, self).__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()
def mousePressEvent(self, event):
self.c.closeApp.emit()
#
# if __name__ == "__main__":
# app = QApplication(sys.argv)
# ex = Example()
# sys.exit(app.exec_())
class Communicate(QObject):
closeApp=pyqtSignal()
class Example(QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.c=Communicate()
self.c.closeApp.connect(self.close)
self.setGeometry(300,300,300,300)
self.setWindowTitle('emit signal')
self.show()
def mousePressEvent(self, event):
self.c.closeApp.emit()
# if __name__ == '__main__':
# app=QApplication()
# ex=Example()
#
# sys.exit(app.exec_())
#对话框QInputDialog
import sys
from PyQt5.QtWidgets import (QWidget, QPushButton, QLineEdit, QInputDialog, QApplication)
#对话框
class Example(QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.btn = QPushButton("对话", 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,'输入对话框','输入名字:')
if ok:
self.le.setText(str(text))
# if __name__ == "__main__":
# app = QApplication(sys.argv)
# ex = Example()
# sys.exit(app.exec_())
import sys
from PyQt5.QtWidgets import (QWidget, QPushButton, QFrame, QColorDialog, QApplication)
from PyQt5.QtGui import QColor
#设置颜色
class Example(QWidget):
def __init__(self):
super(Example, self).__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())
#
# if __name__ == "__main__":
# app = QApplication(sys.argv)
# ex = Example()
# sys.exit(app.exec_())
#
import sys
from PyQt5.QtWidgets import (QWidget, QVBoxLayout, QPushButton,
QSizePolicy, QLabel, QFontDialog, QApplication)
#字体设置
class Example(QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
vbox=QVBoxLayout()
btn=QPushButton('dialog',self)
btn.setSizePolicy(QSizePolicy.Fixed,QSizePolicy.Fixed)
btn.move(20,20)
vbox.addWidget(btn)
vbox.addWidget(btn)
btn.clicked.connect(self.showDialog)
self.lb1 = QLabel("good good study, day day up!.", self)
self.lb1.move(130,20)
vbox.addWidget(self.lb1)
self.setLayout(vbox)
self.setGeometry(300, 300, 250, 180)
self.setWindowTitle("Font dialog")
self.show()
def showDialog(self):
font, ok = QFontDialog.getFont()
if ok:
self.lb1.setFont(font)
# if __name__ == "__main__":
# app = QApplication(sys.argv)
# ex = Example()
# sys.exit(app.exec_())
#
import sys
from PyQt5.QtWidgets import (QMainWindow, QTextEdit, QAction, QFileDialog, QApplication)
from PyQt5.QtGui import QIcon
#打开文件
class Example(QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.textEdit=QTextEdit()
self.setCentralWidget(self.textEdit)
self.statusBar()
openFile=QAction(QIcon('mail.ico'),'open',self)#菜单栏
openFile.setShortcut('ctrl+o')
openFile.triggered.connect(self.showDialog)
menubar=self.menuBar()
fileMenu=menubar.addMenu('FILE')
fileMenu.addAction(openFile)
self.setGeometry(300, 300, 350, 300)
self.setWindowTitle("File dialog")
self.setWindowIcon(QIcon("SmartMedicineKit.png"))
self.show()
def showDialog(self):
fname=QFileDialog.getOpenFileName(self,'打开文件','/')
if fname[0]:
f=open(fname[0],'r')
with f:
data=f.read()
self.textEdit.setText(data)
# if __name__ == "__main__":
# app = QApplication(sys.argv)
# ex = Example()
# sys.exit(app.exec_())
#
import sys
from PyQt5.QtWidgets import QWidget, QCheckBox, QApplication
from PyQt5.QtCore import Qt
#复选框
class Example(QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
cb = QCheckBox('Show title', self)
cb.move(20, 20)
cb.toggle()#勾选复选框
cb.stateChanged.connect(self.changeTitle)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('QCheckBox')
self.show()
def changeTitle(self, state):
if state == Qt.Checked:
self.setWindowTitle('你已经选中我啦')
else:
self.setWindowTitle('为什么还不选我')
# if __name__ == '__main__':
#
# app = QApplication(sys.argv)
# ex = Example()
# sys.exit(app.exec_())
#
import sys
from PyQt5.QtWidgets import (QWidget, QPushButton, QFrame, QApplication)
from PyQt5.QtGui import QColor
#五颜六色
class Example(QWidget):
def __init__(self):
super(Example, self).__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 buttton")
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_())
#
import sys
from PyQt5.QtWidgets import (QWidget, QSlider, QLabel, QApplication)
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap
#滑动条
class Example(QWidget):
def __init__(self):
super(Example, self).__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("mute.png"))
self.label.setGeometry(160, 40, 80, 30)
self.setGeometry(300, 300, 280, 170)
self.setWindowTitle("QSlider")
self.show()
def changeValue(self, value):
if value == 0:
self.label.setPixmap(QPixmap("mute.png"))
elif value > 0 and value <= 30:
self.label.setPixmap(QPixmap("min.png"))
elif value > 30 and value < 80:
self.label.setPixmap(QPixmap("med,png"))
else:
self.label.setPixmap(QPixmap("max.png"))
#
# if __name__ == "__main__":
# app = QApplication(sys.argv)
# ex = Example()
# sys.exit(app.exec_())
#
import sys
from PyQt5.QtWidgets import (QWidget, QProgressBar, QPushButton, QApplication)
from PyQt5.QtCore import QBasicTimer
#滑动条
class Examples(QWidget):
def __init__(self):
super(Examples, self).__init__()
self.initUI()
def initUI(self):
self.pbar = QProgressBar(self)
self.pbar.setGeometry(30, 40, 200, 25)
self.btn = QPushButton("Start", 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("QProssBar")
self.show()
def timerEvent(self, e):
if self.step >= 100:
self.timer.stop()
self.btn.setText("Finished")
return
self.step = self.step + 1
self.pbar.setValue(self.step)
def doAction(self):
if self.timer.isActive():#判断是否是激活状态
self.timer.stop()
self.btn.setText("Start")
else:
self.timer.start(100, self)#100为超时时间,
self.btn.setText("Stop")
# if __name__ == "__main__":
# app = QApplication(sys.argv)
# ex = Examples()
# sys.exit(app.exec_())
#
import sys
from PyQt5.QtWidgets import (QWidget, QCalendarWidget, QLabel, QApplication)
from PyQt5.QtCore import QDate
#日历
class Example(QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
cal = QCalendarWidget(self)#创建日历
cal.setGridVisible(True)
cal.move(20, 20)
cal.clicked[QDate].connect(self.showDate)
self.lb1 = QLabel(self)
date = cal.selectedDate()
self.lb1.setText(date.toString())
self.lb1.move(130, 260)
self.setGeometry(300, 300, 350, 300)
self.setWindowTitle("Calender")
self.show()
def showDate(self, date):
self.lb1.setText(date.toString())
#
# if __name__ == "__main__":
# app = QApplication(sys.argv)
# ex = Example()
# sys.exit(app.exec_())
#绘图
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(Example, self).__init__()
self.initUI()
def initUI(self):
self.text = u'\u041b\u0435\u0432 \u041d\u0438\u043a\u043e\u043b\u0430\
\u0435\u0432\u0438\u0447 \u0422\u043e\u043b\u0441\u0442\u043e\u0439: \n\
\u0410\u043d\u043d\u0430 \u041a\u0430\u0440\u0435\u043d\u0438\u043d\u0430'
self.setGeometry(300, 300, 280, 170)
self.setWindowTitle("Draw text")
self.show()
def paintEvent(self, event):
qp = QPainter()
qp.begin(self)
self.drawText(event,qp)
qp.end()
def drawText(self,event,qp):
qp.setPen(QColor(168,34,3))
qp.setFont(QFont('Decorative',10))
qp.drawText(event.rect(),Qt.AlignCenter,self.text)
#
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
import sys, random
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtGui import QPainter, QColor, QPen
from PyQt5.QtCore import Qt
#散点图
class Example(QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 280, 170)
self.setWindowTitle("Points")
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_())
#
# #
import sys
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtGui import QPainter, QColor, QBrush
class Example(QWidget):
def __init__(self):
super(Example, self).__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_())
#
#
import sys
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtGui import QPainter, QColor, QPen
from PyQt5.QtCore import Qt
class Example(QWidget):
def __init__(self):
super(Example, self).__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_())
import sys
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtGui import QPainter, QBrush
from PyQt5.QtCore import Qt
class Example(QWidget):
def __init__(self):
super(Example, self).__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.Dense3Pattern)
qp.setBrush(brush)
qp.drawRect(10, 105, 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_())
#
import sys
from PyQt5.QtWidgets import (QWidget, QSlider, QApplication, QHBoxLayout, QVBoxLayout)
from PyQt5.QtCore import QObject, Qt, pyqtSignal
from PyQt5.QtGui import QPainter, QFont, QColor, QPen
class Communicate(QObject):
updateBW=pyqtSignal(int)
class BurningWidget(QWidget):
def __init__(self):
super(BurningWidget, self).__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):
font = QFont("Serif", 7, QFont.Light)
qp.setFont(font)
size = self.size()
w = size.width()
h = size.height()
step = int(round(w / 10.0))
till = int(((w / 750.0) * self.value))
full = int(((w / 750.0) * 700))
if self.value >=700:
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(Example, self).__init__()
self.initUI()
def initUI(self):
sld = QSlider(Qt.Horizontal, self)
sld.setFocusPolicy(Qt.NoFocus)
sld.setRange(1, 750)
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_())
#