Flask与PyQt5-Flask控制程序流程? [英] Flask versus PyQt5 - Flask controls program flow?

查看:1291
本文介绍了Flask与PyQt5-Flask控制程序流程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

烧瓶似乎阻止了PyQt5用户界面的更新.

Flask appears to prevent PyQt5 UI from updating.

相应的代码对PyQt5或Flask均适用,但不能一起使用.我知道这可能与设置线程的方式有关.

The respective code works properly for either PyQt5 or Flask - but not together. I understand that it may need to do with the way threading is set up.

任何帮助将不胜感激. TIA.

Any assistance would be greatly appreciated. TIA.

`

import sys
import serial
import threading

from PyQt5.QtWidgets import QWidget, QLabel, QApplication
from flask import Flask, render_template, request, redirect, url_for

app1 = Flask(__name__)

ser = serial.Serial ("/dev/ttyS0", 57600,timeout=3)    #Open port with baud rate

count=0
temp = []

class Example(QWidget):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        global count
        count = 1

        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('PyQt5 vs Flask')
        self.lbl1 = QLabel('Count '+str(count), self)
        self.lbl1.move(100, 50) 
        self.show()

        threading.Timer(5,self.refresh).start()

    def refresh(self):
        global count
        count +=1
        print("UI ",count)

        self.lbl1.setText('Count'+str(count))
        threading.Timer(5,self.refresh).start()

def get_uart():
    global temp
    if ser.inWaiting()>0:
        temp =[str(float(x.decode('utf-8'))) for x in ser.read_until().split(b',')]
        print(temp)
    threading.Timer(1,get_uart).start()

@app1.route("/")
def index():
    global temp  
    templateData = {'temp1' : temp[1] ,'temp2' : temp[2]}
    return render_template('index.html',**templateData)


if __name__ == "__main__":   
    app = QApplication(sys.argv)
    pyqt5 = Example()

    threading.Timer(1,get_uart).start()    
    ser.flushInput()

    #app1.run(host='0.0.0.0',threaded=True, port=5000) # ,debug=True)    

    sys.exit(app.exec_())

`

需要有一个UI来控制要在网站上显示的数据分析.

Need to have a UI to control the data analysis to be displayed on Website.

推荐答案

[已解决]

所有Flask参数可以定义为:

All Flask parameters can be defined as:

port = int(os.environ.get('PORT', local_port))
kwargs = {'host': '127.0.0.1', 'port': port , 'threaded' : True, 'use_reloader': False, 'debug':False}
threading.Thread(target=app.run, daemon = True, kwargs=kwargs).start()

并且Flask不会阻止并使用kwargs中定义的参数运行.

and Flask will NOT Block and run with the parameters defined in kwargs.

这篇关于Flask与PyQt5-Flask控制程序流程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆