Pyqt5 中的设置文本
SetText in Pyqt5
我正在用 pyqt5 开发一个应用程序,我 运行 遇到了一个问题。
有一个接收数据的脚本,在pyqt5中的“main_text.setText (str (TEXT))”行我输出它们,格式为“str” 但是脚本本身每0.2s接收和输出一次数据,但在“main_text.setText (str (TEXT))”行中,它们未更新。
试了定时休眠功能,应用还是不行,
pyqt5中有什么方法可以将不同的数据输出到同一个set文本中?
我的代码:
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys
import MetaTrader5 as mt5
import time
mt5.initialize()
ticket_info = mt5.symbol_info_tick("EURUSD")._asdict()
bid_usd = mt5.symbol_info_tick("EURUSD").bid
def applecation():
app = QApplication(sys.argv)
window = QMainWindow()
window.setWindowTitle('Test Programm')
window.setGeometry(300, 250, 350, 200)
main_text = QtWidgets.QLabel(window)
main_text.setText(str(bid_usd))
main_text.move(100,100)
main_text.adjustSize()
window.show()
sys.exit(app.exec_())
if __name__ == "__main__":
applecation()
你只调用了一次symbol_info_tick方法,所以你只会得到一个数据,如果你想每T秒获取一次该信息,那么你必须使用在辅助线程中执行的while True所以它不会阻止 GUI 并通过信号发送信息。
import sys
import threading
import time
from PyQt5.QtCore import QObject, pyqtSignal
from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow
import MetaTrader5 as mt5
class MetaTrader5Worker(QObject):
dataChanged = pyqtSignal(str)
def start(self):
threading.Thread(target=self._execute, daemon=True).start()
def _execute(self):
mt5.initialize()
while True:
bid_usd = mt5.symbol_info_tick("EURUSD").bid
self.dataChanged.emit(str(bid_usd))
time.sleep(0.5)
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.label = QLabel(self)
self.setWindowTitle("Test Programm")
self.setGeometry(300, 250, 350, 200)
def handle_data_changed(self, text):
self.label.setText(text)
self.label.adjustSize()
def main():
app = QApplication(sys.argv)
window = MainWindow()
window.show()
worker = MetaTrader5Worker()
worker.dataChanged.connect(window.handle_data_changed)
worker.start()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
可以使用threading,比QThreads更容易理解
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
import threading
import sys
import MetaTrader5 as mt5
import time
mt5.initialize()
ticket_info = mt5.symbol_info_tick("EURUSD")._asdict()
bid_usd = mt5.symbol_info_tick("EURUSD").bid
def applecation():
app = QApplication(sys.argv)
window = QMainWindow()
window.setWindowTitle('Test Programm')
window.setGeometry(300, 250, 350, 200)
main_text = QtWidgets.QLabel(window)
main_text.move(100,100)
main_text.adjustSize()
def update_text():
while True:
main_text.setText(str(bid_usd))
time.sleep(0.2)
t1 = threading.Thread(target = update_text())
t1.start()
window.show()
sys.exit(app.exec_())
if __name__ == "__main__":
applecation()
我正在用 pyqt5 开发一个应用程序,我 运行 遇到了一个问题。 有一个接收数据的脚本,在pyqt5中的“main_text.setText (str (TEXT))”行我输出它们,格式为“str” 但是脚本本身每0.2s接收和输出一次数据,但在“main_text.setText (str (TEXT))”行中,它们未更新。 试了定时休眠功能,应用还是不行,
pyqt5中有什么方法可以将不同的数据输出到同一个set文本中?
我的代码:
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys
import MetaTrader5 as mt5
import time
mt5.initialize()
ticket_info = mt5.symbol_info_tick("EURUSD")._asdict()
bid_usd = mt5.symbol_info_tick("EURUSD").bid
def applecation():
app = QApplication(sys.argv)
window = QMainWindow()
window.setWindowTitle('Test Programm')
window.setGeometry(300, 250, 350, 200)
main_text = QtWidgets.QLabel(window)
main_text.setText(str(bid_usd))
main_text.move(100,100)
main_text.adjustSize()
window.show()
sys.exit(app.exec_())
if __name__ == "__main__":
applecation()
你只调用了一次symbol_info_tick方法,所以你只会得到一个数据,如果你想每T秒获取一次该信息,那么你必须使用在辅助线程中执行的while True所以它不会阻止 GUI 并通过信号发送信息。
import sys
import threading
import time
from PyQt5.QtCore import QObject, pyqtSignal
from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow
import MetaTrader5 as mt5
class MetaTrader5Worker(QObject):
dataChanged = pyqtSignal(str)
def start(self):
threading.Thread(target=self._execute, daemon=True).start()
def _execute(self):
mt5.initialize()
while True:
bid_usd = mt5.symbol_info_tick("EURUSD").bid
self.dataChanged.emit(str(bid_usd))
time.sleep(0.5)
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.label = QLabel(self)
self.setWindowTitle("Test Programm")
self.setGeometry(300, 250, 350, 200)
def handle_data_changed(self, text):
self.label.setText(text)
self.label.adjustSize()
def main():
app = QApplication(sys.argv)
window = MainWindow()
window.show()
worker = MetaTrader5Worker()
worker.dataChanged.connect(window.handle_data_changed)
worker.start()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
可以使用threading,比QThreads更容易理解
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
import threading
import sys
import MetaTrader5 as mt5
import time
mt5.initialize()
ticket_info = mt5.symbol_info_tick("EURUSD")._asdict()
bid_usd = mt5.symbol_info_tick("EURUSD").bid
def applecation():
app = QApplication(sys.argv)
window = QMainWindow()
window.setWindowTitle('Test Programm')
window.setGeometry(300, 250, 350, 200)
main_text = QtWidgets.QLabel(window)
main_text.move(100,100)
main_text.adjustSize()
def update_text():
while True:
main_text.setText(str(bid_usd))
time.sleep(0.2)
t1 = threading.Thread(target = update_text())
t1.start()
window.show()
sys.exit(app.exec_())
if __name__ == "__main__":
applecation()