PyQt5和winpython的问题

时间:2015-08-13 10:37:12

标签: python pyqt4 pyqt5

我发布了将进度条码移植到WinPython-64bit-3.4.3.5-Qt5的问题。 它在WinPython-64bit-3.4.3.5中运行良好。 (Qt4版) 我从Spyder做的测试是从Python控制台中击中F5。

可以解释为什么下面的代码使用Qt4而不是Qt5吗?

    # -*- coding: utf-8 -*-
"""
Created on Thu Aug 13 13:00:35 2015

@author: dpenev
"""
import sys

global PYQT
try:
    from PyQt4 import QtGui, QtCore
    PYQT = 4
except:
    from PyQt5 import QtGui, QtCore, QtWidgets
    PYQT = 5

#-------------------------------------------------------------------------------
if PYQT == 5:
    class PB(QtWidgets.QMainWindow):

        def __init__(self):           
            super(PB, self).__init__()
            self.captions=[]
            self.pbs=[]
            self.wds=[]
            self.closedelays=[]
            self.cur=0

        def close(self):
            del(self.captions[self.cur])
            del(self.closedelays[self.cur])
            del(self.pbs[self.cur])
            self.wds[self.cur].close()
            del(self.wds[self.cur])

        def new(self, caption, progress, closedelay):
            self.captions = self.captions + [caption]
            self.cur=len(self.captions)-1
            self.wds.extend([QtWidgets.QWidget()])
            self.closedelays=self.closedelays + [closedelay]
            self.wds[self.cur].setWindowTitle(caption)
            self.wds[self.cur].setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
            self.wds[self.cur].raise_()
            self.pbs.extend([QtWidgets.QProgressBar(self.wds[self.cur])])
            self.pbs[self.cur].setGeometry(30, 40, 200, 25)
            if progress < 100:                              #Update progress           
                self.pbs[self.cur].setValue(abs(progress))
                self.wds[self.cur].show()
                self.wds[self.cur].repaint()
            else:                                           #100% shedule for closing
                self.pbs[self.cur].setValue(100)
                self.wds[self.cur].show()        

                t=QtCore.QTimer()
                t.singleShot(1000*self.closedelays[self.cur], self.close)        

        def update(self, caption, progress):               
            self.cur=self.captions.index(caption)                #Existing progressbar

            if not self.pbs[self.cur].isVisible():               #User has closed the progressbar
                del(self.captions[self.cur])
                del(self.wds[self.cur])
                del(self.pbs[self.cur])
                del(self.closedelays[self.cur])
            else:
                if progress < 100:                              #Update progress
                    self.pbs[self.cur].setValue(abs(progress))
                    self.wds[self.cur].show()
                else:                                           #100%, shedule for closing
                    self.pbs[self.cur].setValue(100)
                    self.wds[self.cur].show()

                    t=QtCore.QTimer()
                    t.singleShot(1000*self.closedelays[self.cur], self.close)
else:
    class PB(QtGui.QMainWindow):

        def __init__(self):           
            super(PB, self).__init__()
            self.captions=[]
            self.pbs=[]
            self.wds=[]
            self.closedelays=[]
            self.cur=0

        def close(self):
            del(self.captions[self.cur])
            del(self.closedelays[self.cur])
            del(self.pbs[self.cur])
            self.wds[self.cur].close()
            del(self.wds[self.cur])

        def new(self, caption, progress, closedelay):
            self.captions = self.captions + [caption]
            self.cur=len(self.captions)-1
            self.wds.extend([QtGui.QWidget()])
            self.closedelays=self.closedelays + [closedelay]
            self.wds[self.cur].setWindowTitle(caption)
            self.wds[self.cur].setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
            self.wds[self.cur].raise_()
            self.pbs.extend([QtGui.QProgressBar(self.wds[self.cur])])
            self.pbs[self.cur].setGeometry(30, 40, 200, 25)
            if progress < 100:                              #Update progress           
                self.pbs[self.cur].setValue(abs(progress))
                self.wds[self.cur].show()
                self.wds[self.cur].repaint()
            else:                                           #100% shedule for closing
                self.pbs[self.cur].setValue(100)
                self.wds[self.cur].show()        

                t=QtCore.QTimer()
                t.singleShot(1000*self.closedelays[self.cur], self.close)        

        def update(self, caption, progress):               
            self.cur=self.captions.index(caption)                #Existing progressbar

            if not self.pbs[self.cur].isVisible():               #User has closed the progressbar
                del(self.captions[self.cur])
                del(self.wds[self.cur])
                del(self.pbs[self.cur])
                del(self.closedelays[self.cur])
            else:
                if progress < 100:                              #Update progress
                    self.pbs[self.cur].setValue(abs(progress))
                    self.wds[self.cur].show()
                else:                                           #100%, shedule for closing
                    self.pbs[self.cur].setValue(100)
                    self.wds[self.cur].show()

                    t=QtCore.QTimer()
                    t.singleShot(1000*self.closedelays[self.cur], self.close)        

if PYQT == 5:
    app=QtWidgets.QApplication.instance()   # checks if QApplication already exists 
    if not app:                         # create QApplication if it doesnt exist
        app = QtWidgets.QApplication(sys.argv)
else:
    app=QtGui.QApplication.instance()   # checks if QApplication already exists 
    if not app:                         # create QApplication if it doesnt exist
        app = QtGui.QApplication(sys.argv) 
pb=PB()

def progressBox(caption,progress,closedelay=1):    
    '''Displays a small window that shows a progress bar.

    Arguments:
    caption: message text
            A different text opens a different progress bar.
    progress: sets the position of the progress bar in % (0...100)
            To update the progress bar use repeated calls with
             the same message text but different progress settings.
            Setting the progress to 100% will automatically 
             close the window after a delay.
    closedelay: delay in seconds.  This is the time that elapses
             between setting progress to 100% and the closing
             of the window.

    This function returns immediately after each call.

    Example:
    import PYnGUInBox as GUI
    GUI.progressBox('Fitting algorithm progress',12)
    '''


    try:
        idx=pb.captions.index(caption)    
        pb.update(caption,progress)
    except:
        pb.new(caption,progress,closedelay)



if __name__ == '__main__':
    import time
    progressBox('Fitting algorithm progress',12)
    time.sleep(3) 
    progressBox('Fitting algorithm progress',50)
    time.sleep(3) 
    progressBox('Fitting algorithm progress',100)

0 个答案:

没有答案