我想要创建一个小型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_())发布于 2019-12-03 10:16:44
函数在python中有自己的作用域:
time = 5
def foo():
time = 10 # only local, not changing the global time.
print(time)
foo() # prints 10
print(time) # prints 5您应该尽可能地避免全局状态。在这里,我将向MyThread对象添加方法和时间属性,并更改该对象的时间值以使其工作。我还将事件移到类中,并添加了一个停止方法。
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_())https://stackoverflow.com/questions/59154337
复制相似问题