首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用QDial更改简单python计时器的时间

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

Stack Overflow用户
提问于 2019-12-03 09:55:01
回答 1查看 158关注 0票数 0

我想要创建一个小型python应用程序,它每n秒钟在控制台中打印一次消息。我还使用PyQt创建用户界面。我希望使用QDial更改消息之间的时间周期,但是当我在sliderMoved方法上更改时间变量的值时,时间不会改变。

代码语言:javascript
复制
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_())
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-03 10:16:44

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

代码语言:javascript
复制
time = 5

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

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

您应该尽可能地避免全局状态。在这里,我将向MyThread对象添加方法和时间属性,并更改该对象的时间值以使其工作。我还将事件移到类中,并添加了一个停止方法。

代码语言:javascript
复制
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_())
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59154337

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档