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

时间:2019-12-03 09:55:01

标签: python multithreading pyqt pyqt5

我想创建一个小的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_())

1 个答案:

答案 0 :(得分:0)

函数在python中有自己的作用域:

time = 5

def foo():
    time = 10 # only local, not changing the global time.
    print(time)

foo()  # prints 10
print(time) # prints 5

您应该尽可能避免全局状态。在这里我添加方法 以及您的MyThread对象的时间属性,并更改此对象的时间值以使其起作用。 我还将事件移到该类中并添加了stop方法。

import sys
from threading import Thread, Event
import time

from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QGridLayout, QDial

START_TIME = time.time()


def elapsed():
    return round(time.time() - START_TIME, 1)


class MyThread(Thread):
    def __init__(self, time):
        super().__init__() # call __init__ of the baseclass
        self.time = time
        self.stopped = Event()

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

    # Thread has a method start, here we add the clearing to the signal
    def start(self):
        print(elapsed(), 'Started')
        self.stopped.clear()
        super().start()

    # new method stop
    def stop(self):
        print(elapsed(), 'Stopped')
        self.stopped.set()


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

        self.thread = MyThread(time=2)
        self.start_btn = QPushButton(self)
        self.start_btn.setText("Start")
        self.start_btn.clicked.connect(self.thread.start)
        layout.addWidget(self.start_btn)

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

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

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


app = QApplication(sys.argv)
screen = Window()
screen.show()
sys.exit(app.exec_())