python实现界面自由切换_PyQt实现界面翻转切换效果

这篇文章主要为大家详细介绍了PyQt实现界面翻转切换效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

PyQt实现界面翻转切换效果是用qt的场景功能来实现的,用到了QGraphicsView,QGraphicsLinearLayout,QGraphicsWidget等有关qt场景的库。算是对qt场景的一个小小的尝试,涉及内容不深,程序效果并是随心所欲,需要去进一步的改善和提高。暂且先把代码贴在此处,供大家学习和指正。

工程包括四个类:

界面A,TestMainWindow,用来充当翻转效果的A面。

界面B,TestMainWindowTwo,用来充当翻转效果的B面。

绘图界面:TestGraphicWidget,用来绘制界面A和B。

主界面:MainWindow,是一个全屏的透明窗口,是整个效果展现的总舞台,内部包含一个QGraphicsScene和一个QGraphicsView,用来展示效果中的界面翻转和界面替换。

整个效果的原理总结为几点:

首先,将整个效果需要的所有界面添加到TestGraphicWidget中,在将TestGraphicWidget放入到QGraphicsScene中,然后经QGraphicsScene添加到主界面中。

然后,界面切换实现,两个函数,非常简单,要显示A,就把B移除并隐藏,要显示B,则把A移除并隐藏。def setOne(self):

self.twoWidget.hide()

self.oneWidget.show()

self.layout.removeItem(self.twoTestWidget)

self.layout.addItem(self.oneTestWidget)

self.view.update()

def setTwo(self):

self.oneWidget.hide()

self.twoWidget.show()

self.layout.removeItem(self.oneTestWidget)

self.layout.addItem(self.twoTestWidget)

self.view.update()

然后是最重要的,翻转效果的实现,用的是TestGraphicWidget特有的翻转方法,参数可以根据实景情况调整。def transeformR(self,count):

r = self.form.boundingRect()

for i in range(1,count):

self.form.setTransform(QTransform()

.translate(r.width() / 2, r.height() / 2)

.rotate(91.00/count*i - 360 * 1, Qt.YAxis)

.translate(-r.width() / 2, -r.height() / 2))

self.waitMethod()

self.view.update()

self.form.setTransform(QTransform()

.translate(r.width() / 2, r.height() / 2)

.rotate(270 - 360 * 1, Qt.YAxis)

.translate(-r.width() / 2, -r.height() / 2))

self.view.update()

if self.formflag %2 == 0:

self.setOne()

else:

self.setTwo()

for i in range(1,count):

self.form.setTransform(QTransform()

.translate(r.width() / 2, r.height() / 2)

.rotate(270 + 93.00/count*i - 360 * 1, Qt.YAxis)

.translate(-r.width() / 2, -r.height() / 2))

self.waitMethod()

self.view.update()

而且提供了两种让程序等待但界面不会卡死的方法:def sleep(self,msec):

dieTime = QTime.currentTime().addMSecs(msec)

print dieTime,QTime.currentTime()

#a = 0

while( QTime.currentTime() < dieTime ):

#print "000000000000"

QCoreApplication.processEvents(QEventLoop.AllEvents, 100)

def waitMethod(self):

tt = QElapsedTimer()

tt.start()

q = QEventLoop()

t = QTimer()

t.setSingleShot(True)

self.connect(t, SIGNAL("timeout()"), q.quit)

t.start(1) # 5s timeout

q.exec_()

if(t.isActive()):

t.stop()

else:

pass

print tt.elapsed()

下面粘上源码,供参考,这个源码可以直接运行,内部的调试信息可以忽略:#coding:utf-8

'''''

Created on 2015 7 15

@author: guowu

'''

from PyQt4.QtGui import QWidget, QTextEdit, QPushButton, QGraphicsScene,\

QGraphicsWidget, QGraphicsLinearLayout, QGraphicsView, QApplication,\

QTransform, QHBoxLayout, QPainter, QLabel, QGraphicsLayoutItem, QFont,\

QPixmap, QBrush

from PyQt4.QtCore import Qt, QTime, QCoreApplication, QEventLoop, QObject,\

SIGNAL, QPoint, QTimer, QBasicTimer, QElapsedTimer, QPointF

import sys

import time

class TestGraphicWidget(QGraphicsWidget):

def __init__(self,parent=None):

super(TestGraphicWidget,self).__init__(parent)

self.setWindowFlags(Qt.Window)

self.setWindowTitle("Turn Widget")

self.resize(400,400)

#self.setPos(QPoint(0,0))

self.mousePressed = False

def closeEvent(self,event):

print "closeclosetest"

self.emit(SIGNAL("startTurn"))

def mouseMoveEvent(self, event):

print "move move"

if self.mousePressed:

#self.move(self.pos() + event.pos() - self.currentPos)

self.setPos(self.pos() + event.pos() - self.currentPos)

def mousePressEvent(self, event):

if event.buttons() == Qt.LeftButton:

self.currentPos = event.pos()

self.mousePressed = True

class TestMainWindow(QWidget):

def __init__(self,parent=None):

super(TestMainWindow,self).__init__(parent)

#self.setStyleSheet("background: transparent;border:0px;")

self.setAttribute(Qt.WA_TranslucentBackground,True)

self.firstButton = QPushButton(u"翻转")

self.secondButton = QPushButton(u"翻转")

self.thirdButton = QPushButton(u"翻转")

self.mainLayout = QHBoxLayout(self)

self.mainLayout.addWidget(self.firstButton)

self.mainLayout.addWidget(self.secondButton)

self.mainLayout.addWidget(self.thirdButton)

self.connect(self.firstButton, SIGNAL("clicked()"), self.startTurn)

self.connect(self.secondButton, SIGNAL("clicked()"), self.startTurn)

self.connect(self.thirdButton, SIGNAL("clicked()"), self.startTurn)

def startTurn(self):

self.emit(SIGNAL("buttonclicked"))

def closeEvent(self,event):

print "closeclosetest"

self.emit(SIGNAL("startTurn"))

def paintEvent(self,event):

#print "paintevent"

painter = QPainter(self)

painter.setRenderHint(QPainter.SmoothPixmapTransform, True)#像素光滑

painter.setRenderHint(QPainter.Antialiasing, True)#反锯齿

pix = QPixmap("cloud-bak.jpg").scaled(self.width(),self.height())

painter.setBrush(QBrush(pix))

painter.drawRoundRect(self.rect(),5,5)

class TestMainWindowTwo(QWidget):

def __init__(self,parent=None):

super(TestMainWindowTwo,self).__init__(parent)

#self.setStyleSheet("QWidget{background: transparent;border:0px;}")

self.setAttribute(Qt.WA_TranslucentBackground,True)

self.firstButton = QPushButton(u"p翻转")

self.secondButton = QPushButton(u"p翻转")

self.thirdButton = QPushButton(u"p翻转")

self.mainLayout = QHBoxLayout(self)

self.mainLayout.addWidget(self.firstButton)

self.mainLayout.addWidget(self.secondButton)

self.mainLayout.addWidget(self.thirdButton)

self.connect(self.firstButton, SIGNAL("clicked()"), self.startTurn)

self.connect(self.secondButton, SIGNAL("clicked()"), self.startTurn)

self.connect(self.thirdButton, SIGNAL("clicked()"), self.startTurn)

def startTurn(self):

self.emit(SIGNAL("buttonclicked"))

def paintEvent(self,event):

#print "paintevent"

painter = QPainter(self)

painter.setRenderHint(QPainter.SmoothPixmapTransform, True)#像素光滑

painter.setRenderHint(QPainter.Antialiasing, True)#反锯齿

pix = QPixmap("login.jpg").scaled(self.width(),self.height())

painter.setBrush(QBrush(pix))

painter.drawRoundRect(self.rect(),5,5)

class MainWindow(QWidget):

def __init__(self,parent=None):

super(MainWindow,self).__init__(parent)

#self.setStyleSheet("QGraphicsView{background:rgb(0,0,0,0);border:0px;}")

self.formflag = 0

self.scene = QGraphicsScene()

self.setWindowFlags(Qt.FramelessWindowHint|Qt.WindowStaysOnTopHint)

self.setAttribute(Qt.WA_TranslucentBackground,True)

#创建部件,并关联它们的信号和槽

self.oneWidget = TestMainWindow()

self.connect(self.oneWidget, SIGNAL("buttonclicked"),self.startTurn)

self.twoWidget = TestMainWindowTwo()

self.connect(self.twoWidget, SIGNAL("buttonclicked"),self.startTurn)

#self.textEdit = QGraphicsLayoutItem(self.edit)

self.oneTestWidget = self.scene.addWidget(self.oneWidget)

self.twoTestWidget = self.scene.addWidget(self.twoWidget)

self.form = TestGraphicWidget()

self.connect(self.form, SIGNAL("startTurn"),self.close)

#将部件添加到布局管理器中

self.layout = QGraphicsLinearLayout(self.form)

self.layout.setSpacing(0)

self.layout.addItem(self.oneTestWidget)

self.layout.addItem(self.twoTestWidget)

self.layout.removeItem(self.twoTestWidget)

self.twoWidget.hide()

#创建图形部件,设置其为一个顶层窗口,然后在其上应用布局

#self.form.setWindowFlags(Qt.Window|Qt.FramelessWindowHint)

#self.form.setWindowTitle("Widget Item")

#self.form.setLayout(layout)

self.scene.addItem(self.form)

#self.form.setPos(QPointF(0,0))

#self.form.hide()

self.view = QGraphicsView(self.scene,self)

#self.view.setScene(self.scene)

self.view.setRenderHint(QPainter.Antialiasing)

self.view.setViewportUpdateMode(QGraphicsView.BoundingRectViewportUpdate)

self.view.resize(QApplication.desktop().width(),QApplication.desktop().height())

self.view.setStyleSheet("background: transparent;border:0px;")

self.view.setWindowFlags(Qt.FramelessWindowHint)

self.view.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)

self.view.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)

self.view.move(QPoint(0,0))

#self.view.setAttribute(Qt.WA_TranslucentBackground,True)

#self.form.resize(500,500)

#self.form.setWindowFlags(Qt.FramelessWindowHint)

#for(int i=1;i<=360;i++)

def setOne(self):

self.twoWidget.hide()

self.oneWidget.show()

self.layout.removeItem(self.twoTestWidget)

self.layout.addItem(self.oneTestWidget)

self.view.update()

def setTwo(self):

self.oneWidget.hide()

self.twoWidget.show()

self.layout.removeItem(self.oneTestWidget)

self.layout.addItem(self.twoTestWidget)

self.view.update()

def transeformT(self,count):

r = self.form.boundingRect()

for i in range(1,count):

print "............."

self.form.setTransform(QTransform()

.translate(r.width() / 2, r.height() / 2)

.rotate(364.00/count*i - 360 * 1, Qt.YAxis)

.translate(-r.width() / 2, -r.height() / 2))

self.waitMethod()

#self.sleep(1)

#time.sleep(1)

self.view.update()

#

def transeformS(self,count):

r = self.form.boundingRect()

for i in range(1,count):

print "............."

self.form.setTransform(QTransform()

.translate(r.width() / 2, r.height() / 2)

.rotate(182.00/count*i - 360 * 1, Qt.YAxis)

.translate(-r.width() / 2, -r.height() / 2))

self.waitMethod()

self.view.update()

def transeformR(self,count):

r = self.form.boundingRect()

for i in range(1,count):

print "............."

self.form.setTransform(QTransform()

.translate(r.width() / 2, r.height() / 2)

.rotate(91.00/count*i - 360 * 1, Qt.YAxis)

.translate(-r.width() / 2, -r.height() / 2))

self.waitMethod()

self.view.update()

self.form.setTransform(QTransform()

.translate(r.width() / 2, r.height() / 2)

.rotate(270 - 360 * 1, Qt.YAxis)

.translate(-r.width() / 2, -r.height() / 2))

self.view.update()

if self.formflag %2 == 0:

self.setOne()

else:

self.setTwo()

for i in range(1,count):

self.form.setTransform(QTransform()

.translate(r.width() / 2, r.height() / 2)

.rotate(270 + 93.00/count*i - 360 * 1, Qt.YAxis)

.translate(-r.width() / 2, -r.height() / 2))

self.waitMethod()

self.view.update()

def transeformB(self,count):

r = self.form.boundingRect()

for i in range(1,count):

print "............."

self.form.setTransform(QTransform()

.translate(r.width(), r.height())

.rotate(91.00/count*i - 360 * 1, Qt.YAxis)

.translate(-r.width(), -r.height()))

self.waitMethod()

self.view.update()

self.form.setTransform(QTransform()

.translate(r.width(), r.height())

.rotate(270 - 360 * 1, Qt.YAxis)

.translate(-r.width(), -r.height()))

self.view.update()

for i in range(1,count):

self.form.setTransform(QTransform()

.translate(r.width(), r.height())

.rotate(270 + 93.00/count*i - 360 * 1, Qt.YAxis)

.translate(-r.width(), -r.height()))

self.waitMethod()

self.view.update()

def transeform(self):

print self.form.pos()

#self.scene.itemAt(QPointF)

rxx = self.scene.itemsBoundingRect()

rx = self.form.boundingRect()

r = self.form.geometry()

print r,rx,rxx

for i in range(1,361):

print self.form.pos()

print "............."

#print r.width(),r.height()

transform = QTransform()

transform.translate(r.width() / 2, r.height()/2)#中心点,原点

transform.rotate(i - 360 * 1, Qt.YAxis)#绕X轴旋转角度

self.form.setTransform(transform)

# self.form.setTransform(QTransform()

# .translate(r.width() / 2, r.height() / 2)

# .rotate(i - 360 * 1, Qt.YAxis)

# .translate(-r.width() / 2, -r.height() / 2))

# self.form.setTransform(QTransform()

# .translate(250, 250)

# .rotate(i - 360 * 1, Qt.YAxis)

# .translate(-250, -250))

self.waitMethod()

self.view.update()

#

def startTurn(self):

self.formflag += 1

self.transeformR(30)

#self.transeform()

#self.form.close()

#self.view.close()

def closeEvent(self,event):

print "close"

self.form.close()

self.view.close()

self.close()

def sleep(self,msec):

dieTime = QTime.currentTime().addMSecs(msec)

print dieTime,QTime.currentTime()

#a = 0

while( QTime.currentTime() < dieTime ):

#print "000000000000"

QCoreApplication.processEvents(QEventLoop.AllEvents, 100)

def waitMethod(self):

tt = QElapsedTimer()

tt.start()

q = QEventLoop()

t = QTimer()

t.setSingleShot(True)

self.connect(t, SIGNAL("timeout()"), q.quit)

t.start(1) # 5s timeout

q.exec_()

if(t.isActive()):

t.stop()

else:

pass

print tt.elapsed()

if __name__ == "__main__":

app = QApplication(sys.argv)

font = QFont()

font.setPointSize(16)

font.setFamily(("Roman Times"))

app.setFont(font)

c = MainWindow()

c.show()

c.move(QPoint(0,0))

app.exec_()

相关推荐:

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Python Qt5是一种基于Python编程语言的开发工具,它通过PyQt5库来实现。通过Python Qt5,可以很容易地开发出具有现代风格和人性化交互界面的应用程序。 Python Qt5提供了大量的示例,包括各种功能模块,如图像处理、文件处理、网络通信、多媒体播放等。这些示例都可以帮助开发者更好地理解和掌握Python Qt5的使用方法。 例如,Qt5提供了一个文本编辑器示例,可以通过Python Qt5来实现。这个示例包括了文本编辑区,菜单和工具栏等控件,同时还可以支持多文档编辑和代码高亮等功能。这个示例可以帮助开发者了解如何使用Python Qt5来创建基本的文本编辑器,并根据需要进行调整和扩展。 另一个实例是Qt5中的图片转换器。这个示例可以将图片转换成另一种格式,支持多种图片格式,如png、jpg、bmp等。此外,它还提供了许多高级选项,如变换和旋转等。这个示例可以帮助开发者了解如何使用Python Qt5实现图片处理功能。 总之,Python Qt5是一个非常强大的工具,可以帮助开发者快速地创建美观的应用程序,并提供丰富的示例来帮助开发者更好地理解和掌握使用方法。 ### 回答2: Python Qt5是一个非常流行的Python框架,允许开发人员使用Python编写高效、交互式、灵活的用户界面Python Qt5提供了Qt5 C++框架的Python绑定,并且这个框架也能够访问许多Qt5功能,如Widgets、GUI元素、数据库、网络和图形等。 Python Qt5实例可以用于很多不同的应用程序,如窗口控制、用户界面设计、数据分析和可视化、网络通信和多线程处理等。Python Qt5实例通常采用Qt Designer或者PyQt5 Designer这样的可视化工具,来使得用户接口的设计更加方便,同时也可以自动的连接Python代码。 需要注意的是,Python Qt5实例在GUI的设计和交互上可以带来很大的方便,但是如果想要更深入的Qt5开发,还需要了解更多Qt5的C++底层实现Python Qt5的底层实现。因此在开发Python Qt5实例时,也需要注意代码的可扩展性和性能优化,以达到更好的用户体验和更高的可维护性。 ### 回答3: Python Qt5实例是使用Python语言和Qt5图形界面库创建应用程序的具体实现示例。Qt是一个跨平台的图形用户界面开发框架,而Python则是一种高级编程语言,二者的结合可以创建出高效、易于维护的应用程序。 创建Python Qt5实例需要掌握Qt5框架的基础知识和Python编程语言的特性。Python Qt5实例可以涵盖许多领域,例如GUI编程、网络编程、多线程、媒体处理等。在创建Python Qt5实例时,需要考虑用户体验、性能、可移植性等因素,以实现一个完整、协调、高效的应用程序。 Python Qt5实例的常见应用包括:桌面应用程序、游戏、数据可视化工具、音视频播放器、图像处理工具、网络爬虫等。 总之,Python Qt5实例是使用Python和Qt5框架的应用程序实现示例,能够让开发者在创建高效、易于维护的应用程序时更加得心应手。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值