猿问

按下按钮时在 Kivy 应用程序中运行 Flask 应用程序

有没有办法可以在 Kivy 应用程序中同时运行 Kivy 和 Flask?此外,我需要该应用程序,因此一旦您单击 Kivy 应用程序中的按钮,就会触发启动 Flask 网页的功能。然后,使用Python内置的webbrowser模块,我需要它在默认浏览器中自动打开网页。


运行此代码时,我没有收到任何错误。只是 Kivy 应用程序冻结并且不再响应。


到目前为止我的代码:


from kivy.app import App

from kivy.lang import Builder

from kivy.uix.screenmanager import ScreenManager, Screen

from flask import Flask

from werkzeug.serving import run_simple

import webbrowser


Builder.load_file('design.kv')


answers = []


class CalcScreen(Screen):

    def list_view(self):

        self.manager.current = "list_screen"

    def cround_view(self):

        self.manager.current = "round_calc_screen"

    def calculate(self):

        LengthVal = float(self.ids.length.text)

        WidthVal = float(self.ids.width.text)

        ThicknessVal = float(self.ids.thickness.text)


        FinalCalc = LengthVal * WidthVal * ThicknessVal / 144

        FinalCalc = round(FinalCalc,1)

        answers.append(FinalCalc)


        self.ids.board_feet.text = str(FinalCalc)


class ListScreen(Screen):

    def calc_view(self):

        self.manager.current = "calc_screen"

    def UpdateInfo(self):

        tot = 0

        for num in answers:

            tot += num


        self.ids.total_board_feet.text = str(round(tot,1))

        self.ids.total_boards.text = str(len(answers))

        self.ids.list.text = str(', '.join(map(str, answers)))

    def ClearListAnswers(self):

        answers.clear()

    def printerview(self):

        app = Flask(__name__)


        @app.route('/')

        def home():

            return f"<h1>BFCalc Printer Friendly View</h1>\n{self.ids.list.text}"


        run_simple('localhost',5000,app)


        webbrowser.open_new('localhost:5000')


class RoundCalcScreen(Screen):

    def calc_view(self):

        self.manager.current = "calc_screen"

    def rc_calculate(self):

        RC_DiameterVal = float(self.ids.rc_diameter.text)

        RC_RadiusVal = RC_DiameterVal / 2

        RC_ThicknessVal = float(self.ids.rc_thickness.text)



墨色风雨
浏览 129回答 1
1回答

不负相思意

以这种方式使用后端框架是一种不好的做法(它们根本不以这种方式使用)。这取决于您的需求,您可以尝试使用纯 HTML 代替。def printerview(self):&nbsp; &nbsp; import webbrowser&nbsp; &nbsp; file_name = "my_html.html"&nbsp; &nbsp; html = f"""<h1>BFCalc Printer Friendly View</h1>\n{self.ids.list.text}"""&nbsp; &nbsp; with open(file_name, "w+") as f:&nbsp; &nbsp; &nbsp; &nbsp; f.write(html)&nbsp; &nbsp; # open html in a browser&nbsp; &nbsp; webbrowser.open(file_name)
随时随地看视频慕课网APP

相关分类

Python
我要回答