有沒有辦法可以在 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)
添加回答
舉報
0/150
提交
取消