class ProgressBar(QWidget):
    def __init__(self,parent=None):
        super(ProgressBar, self).__init__(parent)
        self.initUI()
    def initUI(self):
        self.pbar=QProgressBar(self)
        self.pbar.setGeometry(40,40,200,25)

        self.btn=QPushButton('开始',self)
        self.btn.move(40,80)
        self.btn.clicked.connect(self.doAction)

        self.timer=QBasicTimer()
        self.value=0
        self.setGeometry(300,300,280,170)
        self.setWindowTitle('QProgressBar控件')
        self.show()
    def timerEvent(self, e):    #递归?
        if self.value>=100:
            self.timer.stop()
            self.btn.setText('完成')
            return
        self.value=self.value+1
        self.pbar.setValue(self.value)

    def doAction(self):
        if self.timer.isActive():
            self.timer.stop()
            self.btn.setText('开始')
        else:
            self.timer.start(100,self)
            self.btn.setText('停止')
Пример #2
0
class Example(QWidget):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):

        vbox = QVBoxLayout()

        self.pbar = QProgressBar()  # 3.04 加入布局可省略self !!!
        # self.pbar.setGeometry(30, 40, 200, 25)
        vbox.addWidget(self.pbar)

        self.btn = QPushButton('Start')
        # self.btn.move(40, 80)
        self.btn.clicked.connect(self.doAction)  # 6.02 对比click[bool]
        vbox.addWidget(self.btn)

        self.setLayout(vbox)

        self.timer = QBasicTimer()
        self.step = 0

        self.setGeometry(300, 300, 600, 400)
        self.setWindowTitle('QPressBar')
        self.show()

    # 重写原生槽函数. 没过一个单位时间, 执行一次
    def timerEvent(self, event):

        # 到达100%时, 即便点了按钮, 也会马上变为Finished
        if self.step >= 100:
            self.timer.stop()
            self.btn.setText('Finished')
            return

        self.step = self.step + 1
        self.pbar.setValue(self.step)

    def doAction(self):

        if self.timer.isActive():
            self.timer.stop()
            self.btn.setText('Start')
        else:
            self.timer.start(100, self)  # 绑定self, 为self及时, 时限100s
            self.btn.setText('Stop')
Пример #3
0
class TapeWidget(QWidget):

    X_MARGIN = 0.0
    Y_MARGIN = 5.0

    def __init__(self, parent=None):
        super().__init__(parent)
        self.setFocusPolicy(Qt.StrongFocus)
        self.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.Fixed)
        self._timer = QBasicTimer()
        self._move = 0
        self._displace = 0
        self._seek_accel = 0
        self._header_pos = 0
        self._is_seeking = False
        self._speed = 1.0
        # self._font = QFont("monospaced")
        # self._font.setStyleHint(QFont.TypeWriter)
        # self._font.setPointSize(11)

    @property
    def speed(self):
        return self._speed

    @speed.setter
    def speed(self, value: float):
        if value <= 0:
            raise
        self._speed = value

    def move_to(self, position: int):
        if self._header_pos == position and self._move == position:
            return
        self._move = position
        self._is_seeking = True
        self._seek_accel = abs(self._header_pos - position) * self._speed
        if not self._timer.isActive():
            self._timer.start(10, self)
        self.update()

    def move_right(self):
        self.move_to(self._move + 1)

    def move_left(self):
        self.move_to(self._move - 1)

    def keyPressEvent(self, event):
        if event.key() == Qt.Key_Left:
            self.move_left()
        elif event.key() == Qt.Key_Right:
            self.move_right()

    def timerEvent(self, event):
        if not self._is_seeking:
            self._timer.stop()
            return
        if self._move < self._header_pos:
            self._displace += self._seek_accel
            if abs(self._displace) >= 20:
                self._header_pos -= 1
                self._displace = 0
        else:
            self._displace -= self._seek_accel
            if abs(self._displace) >= 20:
                self._header_pos += 1
                self._displace = 0

        if self._header_pos == self._move:
            self._timer.stop()
            self._is_seeking = False
        self.update()

    def sizeHint(self):
        return self.minimumSizeHint()

    def minimumSizeHint(self):
        return QSize(0, 40)

    def paintEvent(self, event):
        painter = QPainter(self)
        painter.setPen(Qt.white)
        painter.setBrush(Qt.white)
        painter.drawRect(event.rect())
        painter.setPen(Qt.lightGray)
        r = QRect(event.rect()).adjusted(TapeWidget.X_MARGIN,
                                         TapeWidget.Y_MARGIN,
                                         -TapeWidget.X_MARGIN,
                                         -TapeWidget.Y_MARGIN)
        # Draw tape
        painter.fillRect(r, QColor(Qt.gray).light(150))
        painter.drawLine(r.topLeft(), r.topRight())
        painter.drawLine(r.bottomLeft(), r.bottomRight())

        fm = QFontMetrics(self.font())
        center = QPoint(self.width() / 2, self.height() / 2)

        cw = fm.width(' ') + 15
        # ch = fm.height() + 5

        x = self._displace + center.x() + cw / 2
        while x > 0:
            x -= cw
            painter.drawLine(x, r.y() + 5, x, r.bottom() - 5)

        x = self._displace + center.x() - cw / 2
        while x < self.width():
            x += cw
            painter.drawLine(x, r.y() + 5, x, r.bottom() - 5)

        # Draw head
        color = QColor('#00a8ff')
        color.setAlpha(100)
        painter.setBrush(color)
        painter.drawRect(center.x() - 15, 3, 29, 33)