PyQt實(shí)現(xiàn)界面翻轉(zhuǎn)切換效果
PyQt實(shí)現(xiàn)界面翻轉(zhuǎn)切換效果是用qt的場(chǎng)景功能來實(shí)現(xiàn)的,用到了QGraphicsView,QGraphicsLinearLayout,QGraphicsWidget等有關(guān)qt場(chǎng)景的庫。算是對(duì)qt場(chǎng)景的一個(gè)小小的嘗試,涉及內(nèi)容不深,程序效果并是隨心所欲,需要去進(jìn)一步的改善和提高。暫且先把代碼貼在此處,供大家學(xué)習(xí)和指正。
工程包括四個(gè)類:
界面A,TestMainWindow,用來充當(dāng)翻轉(zhuǎn)效果的A面。
界面B,TestMainWindowTwo,用來充當(dāng)翻轉(zhuǎn)效果的B面。
繪圖界面:TestGraphicWidget,用來繪制界面A和B。
主界面:MainWindow,是一個(gè)全屏的透明窗口,是整個(gè)效果展現(xiàn)的總舞臺(tái),內(nèi)部包含一個(gè)QGraphicsScene和一個(gè)QGraphicsView,用來展示效果中的界面翻轉(zhuǎn)和界面替換。

整個(gè)效果的原理總結(jié)為幾點(diǎn):
首先,將整個(gè)效果需要的所有界面添加到TestGraphicWidget中,在將TestGraphicWidget放入到QGraphicsScene中,然后經(jīng)QGraphicsScene添加到主界面中。
然后,界面切換實(shí)現(xiàn),兩個(gè)函數(shù),非常簡(jiǎn)單,要顯示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()
然后是最重要的,翻轉(zhuǎn)效果的實(shí)現(xiàn),用的是TestGraphicWidget特有的翻轉(zhuǎn)方法,參數(shù)可以根據(jù)實(shí)景情況調(diào)整。
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()
而且提供了兩種讓程序等待但界面不會(huì)卡死的方法:
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()
下面粘上源碼,供參考,這個(gè)源碼可以直接運(yùn)行,內(nèi)部的調(diào)試信息可以忽略:
#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"翻轉(zhuǎn)")
self.secondButton = QPushButton(u"翻轉(zhuǎn)")
self.thirdButton = QPushButton(u"翻轉(zhuǎn)")
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翻轉(zhuǎn)")
self.secondButton = QPushButton(u"p翻轉(zhuǎn)")
self.thirdButton = QPushButton(u"p翻轉(zhuǎn)")
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)
#創(chuàng)建部件,并關(guān)聯(lián)它們的信號(hào)和槽
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()
#創(chuàng)建圖形部件,設(shè)置其為一個(gè)頂層窗口,然后在其上應(yīng)用布局
#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)#中心點(diǎn),原點(diǎn)
transform.rotate(i - 360 * 1, Qt.YAxis)#繞X軸旋轉(zhuǎn)角度
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_()
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
本地文件上傳到七牛云服務(wù)器示例(七牛云存儲(chǔ))
這篇文章主要介紹了使用PYTHON把本地文件上傳到七牛云服務(wù)的方法,開發(fā)環(huán)境是Python 2.7,大家參考使用吧2014-01-01
Python利用partial偏函數(shù)生成不同的聚合函數(shù)
本文主要介紹了Python利用partial偏函數(shù)生成不同的聚合函數(shù),利用偏函數(shù)的概念,可以生成一些新的函數(shù),在調(diào)用這些新函數(shù)時(shí),不用再傳遞固定值的參數(shù),這樣可以使代碼更簡(jiǎn)潔,感興趣的可以了解一下2024-03-03
Python使用struct處理二進(jìn)制的實(shí)例詳解
這篇文章主要介紹了Python使用struct處理二進(jìn)制的實(shí)例詳解的相關(guān)資料,希望通過本文大家能掌握這部分內(nèi)容,需要的朋友可以參考下2017-09-09
eclipse創(chuàng)建python項(xiàng)目步驟詳解
在本篇內(nèi)容里小編給大家分享了關(guān)于eclipse創(chuàng)建python項(xiàng)目的具體步驟和方法,需要的朋友們跟著學(xué)習(xí)下。2019-05-05
Python讀取DataFrame的某行或某列的方法實(shí)現(xiàn)
Dataframe是Python中一種重要的數(shù)據(jù)處理工具,它能夠以表格形式存儲(chǔ)并處理數(shù)據(jù),本文主要介紹了Python讀取DataFrame的某行或某列的方法實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-04-04
Python企業(yè)編碼生成系統(tǒng)總體系統(tǒng)設(shè)計(jì)概述
這篇文章主要介紹了Python企業(yè)編碼生成系統(tǒng)總體系統(tǒng)設(shè)計(jì),簡(jiǎn)單描述了Python企業(yè)編碼生成系統(tǒng)的功能、結(jié)構(gòu)與相關(guān)編碼實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-07-07
基于Python實(shí)現(xiàn)簡(jiǎn)單排行榜功能
排行榜是一種常見的功能,它可以用于展示和比較數(shù)據(jù)的排名或分?jǐn)?shù),本文將詳細(xì)介紹如何使用Python實(shí)現(xiàn)排行榜功能,感興趣的小伙伴可以了解一下2024-02-02
基于Python實(shí)現(xiàn)一個(gè)簡(jiǎn)單的敏感詞過濾功能
這篇文章主要介紹了Python實(shí)現(xiàn)敏感詞過濾功能的示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)我們學(xué)習(xí)python有定的幫助,感興趣的小伙伴們可以參考一下2023-06-06
Python設(shè)計(jì)模式之觀察者模式原理與用法詳解
這篇文章主要介紹了Python設(shè)計(jì)模式之觀察者模式,簡(jiǎn)單講述了觀察者模式的概念、原理并結(jié)合實(shí)例形式分析了Python實(shí)現(xiàn)與使用方法,需要的朋友可以參考下2019-01-01

