亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

按下按鈕時在 Kivy 應用程序中運行 Flask 應用程序

按下按鈕時在 Kivy 應用程序中運行 Flask 應用程序

墨色風雨 2023-08-03 17:09:33
有沒有辦法可以在 Kivy 應用程序中同時運行 Kivy 和 Flask?此外,我需要該應用程序,因此一旦您單擊 Kivy 應用程序中的按鈕,就會觸發啟動 Flask 網頁的功能。然后,使用Python內置的webbrowser模塊,我需要它在默認瀏覽器中自動打開網頁。運行此代碼時,我沒有收到任何錯誤。只是 Kivy 應用程序凍結并且不再響應。到目前為止我的代碼:from kivy.app import Appfrom kivy.lang import Builderfrom kivy.uix.screenmanager import ScreenManager, Screenfrom flask import Flaskfrom werkzeug.serving import run_simpleimport webbrowserBuilder.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)
查看完整描述

1 回答

?
不負相思意

TA貢獻1777條經驗 獲得超10個贊

以這種方式使用后端框架是一種不好的做法(它們根本不以這種方式使用)。這取決于您的需求,您可以嘗試使用純 HTML 代替。


def printerview(self):

    import webbrowser


    file_name = "my_html.html"

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

    with open(file_name, "w+") as f:

        f.write(html)

    # open html in a browser

    webbrowser.open(file_name)


查看完整回答
反對 回復 2023-08-03
  • 1 回答
  • 0 關注
  • 160 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號