1.
导入
pip install pyqt5
1.pyQt代码
1.
# -*- coding:utf-8 -*-
# Time : 2019/09/24 下午 4:29
# Author : 御承扬
# e-mail:2923616405@qq.com
# project: PyQt5
# File : winDraw02.py
# @software: PyCharm
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import sys
class WinForm(QWidget):
def __init__(self, parent=None):
super(WinForm, self).__init__(parent)
self.setWindowTitle("绘制矩形示例")
self.setWindowIcon(QIcon("./images/Python2.ico"))
self.pix = QPixmap()
self.lastPoint = QPoint()
self.endPoint = QPoint()
self.initUI()
def initUI(self):
self.resize(600, 500)
self.pix = QPixmap(400, 400)
self.pix.fill(Qt.white)
def paintEvent(self, event):
painter = QPainter(self)
x = self.lastPoint.x()
y = self.lastPoint.y()
w = self.endPoint.x() - x
h = self.endPoint.y() - y
pp = QPainter(self.pix)
pp.drawRect(x, y, w, h)
painter.drawPixmap(0, 0, self.pix)
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.lastPoint = event.pos()
self.endPoint = self.lastPoint
def mouseMoveEvent(self, event):
if event.buttons() and Qt.LeftButton:
self.endPoint = event.pos()
self.update()
def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton:
self.endPoint = event.pos()
self.update()
if __name__ == "__main__":
app = QApplication(sys.argv)
form = WinForm()
form.show()
sys.exit(app.exec_())
2.
import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtCore import QPropertyAnimation, QRect
class AnimatedWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(100, 100, 200, 200)
self.setWindowTitle('PyQt Animation Example')
# 创建一个动画对象,指定动画的目标对象和属性
self.anim = QPropertyAnimation(self, b"geometry")
self.anim.setDuration(1000) # 设置动画持续时间,以毫秒为单位
self.anim.setStartValue(QRect(100, 100, 0, 0)) # 设置动画的起始值
self.anim.setEndValue(QRect(100, 100, 1800, 1800)) # 设置动画的结束值
# 开始动画
self.anim.start()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = AnimatedWindow()
ex.show()
sys.exit(app.exec_())
3.
import sys
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtGui import QIcon, QBitmap, QPainter, QPixmap, QCursor, QMovie, QImage
from PyQt5.QtWidgets import QHBoxLayout, QPushButton, QMessageBox, QApplication, QVBoxLayout, QWidget, \
QLabel, QGridLayout, QLineEdit, QTextEdit, QFormLayout, QComboBox
class OpacityWindowDemo(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# 设置定位和左上角坐标
self.setGeometry(300, 300, 360, 260)
# 设置窗口标题
self.setWindowTitle('透明窗口')
# 设置窗口图标
# self.setWindowIcon(QIcon('../web.ico'))
if __name__ == '__main__':
app = QApplication(sys.argv)
# 设置应用图标
app.setWindowIcon(QIcon('../web.ico'))
w = OpacityWindowDemo()
# 0-1,1表示不透明,0表示完全透明
w.setWindowOpacity(0.09)
w.show()
sys.exit(app.exec_())
tk代码
1.
# 导入所需的库
from tkinter import *
# 创建一个tkinter窗口实例
win=Tk()
# 设置窗口大小
win.geometry("700x350")
# 设置窗口大小为固定
win.resizable(False,False)
# 创建一个画布部件
canvas=Canvas(win, width=700, height=350)
canvas.pack()
# 在画布部件中创建一个椭圆形或球
ball=canvas.create_oval(10,10,50,50, fill="green3")
# 移动球
xspeed=yspeed=3
def move_ball():
global xspeed, yspeed
canvas.move(ball, xspeed, yspeed)
(leftpos, toppos, rightpos, bottompos)=canvas.coords(ball)
if leftpos <=0 or rightpos>=700:
xspeed=-xspeed
if toppos <=0 or bottompos >=350:
yspeed=-yspeed
canvas.after(30,move_ball)
canvas.after(30, move_ball)
win.mainloop()
2.
from tkinter import *
from tkinter.ttk import *
ws=Tk()
Progress_Bar=Progressbar(ws,orient=HORIZONTAL,length=250,mode='determinate')
def Slide():
import time
Progress_Bar['value']=20
ws.update_idletasks()
time.sleep(1)
Progress_Bar['value']=50
ws.update_idletasks()
time.sleep(1)
Progress_Bar['value']=80
ws.update_idletasks()
time.sleep(1)
Progress_Bar['value']=100
Progress_Bar.pack()
Button(ws,text='Run',command=Slide).pack(pady=10)
mainloop()