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