使用QDial更改简单python计时器的时间

问题描述 投票:0回答:1

我想创建一个小的python应用程序,每隔n秒在控制台中打印一条消息。我还使用PyQt创建用户界面。我想使用QDial来更改消息之间的时间段,但是当我在sliderMoved方法上更改时间变量的值时,时间不会改变。

import sys
from threading import Timer,Thread,Event
from datetime import datetime

from PyQt5.QtWidgets import QApplication, QLabel, QPushButton
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QGridLayout, QDial
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot

stopFlag = Event()
time = 2

class MyThread(Thread):

    def __init__(self, event):
        Thread.__init__(self)
        self.stopped = event

    def run(self):
        while not self.stopped.wait(time):
            print('Message')


def startThread():
    stopFlag.clear()
    thread = MyThread(stopFlag)
    thread.start()

def start_clicked():
    print("Start clicked")
    startThread()

def stop_clicked():
    print("Stop clicked")   
    stopFlag.set()


class Window(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        layout = QGridLayout()
        self.setLayout(layout)

        self.start_btn = QPushButton(self)
        self.start_btn.setText("Start")
        self.start_btn.clicked.connect(start_clicked)
        layout.addWidget(self.start_btn)

        self.stop_btn = QPushButton(self)
        self.stop_btn.setText("Stop")
        self.stop_btn.clicked.connect(stop_clicked)
        layout.addWidget(self.stop_btn)

        self.dial = QDial()
        self.dial.setMinimum(1)
        self.dial.setMaximum(10)
        self.dial.setValue(2)
        self.dial.valueChanged.connect(self.sliderMoved)
        layout.addWidget(self.dial)

    def sliderMoved(self):
        print("Dial value = %i" % (self.dial.value()))
        time = self.dial.value()

app = QApplication(sys.argv)
screen = Window()
screen.show()
sys.exit(app.exec_())
python multithreading pyqt pyqt5
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.