{"id":1186108,"date":"2025-01-15T19:48:52","date_gmt":"2025-01-15T11:48:52","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1186108.html"},"modified":"2025-01-15T19:48:54","modified_gmt":"2025-01-15T11:48:54","slug":"python%e5%a6%82%e4%bd%95%e7%bc%96%e5%86%99%e5%ae%89%e5%8d%93%e7%a8%8b%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1186108.html","title":{"rendered":"python\u5982\u4f55\u7f16\u5199\u5b89\u5353\u7a0b\u5e8f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25135019\/1cc295e2-d70e-45e0-8b30-3a0ce81d22ec.webp\" alt=\"python\u5982\u4f55\u7f16\u5199\u5b89\u5353\u7a0b\u5e8f\" \/><\/p>\n<p><p> <strong>Python\u7f16\u5199\u5b89\u5353\u7a0b\u5e8f\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528Kivy\u3001BeeWare\u3001Pyqtdeploy\u548cSL4A\u7b49\u3002<\/strong>\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\u7684\u4e00\u79cd\u2014\u2014Kivy\u7684\u4f7f\u7528\u3002Kivy\u662f\u4e00\u4e2a\u5f00\u6e90\u7684Python\u5e93\uff0c\u4e13\u95e8\u7528\u4e8e\u5f00\u53d1\u8de8\u5e73\u53f0\u5e94\u7528\uff0c\u5305\u62ec\u5b89\u5353\u5e94\u7528\u3002<strong>Kivy\u7684\u4f18\u52bf\u5728\u4e8e\u5176\u7b80\u5355\u6613\u5b66\u3001\u652f\u6301\u591a\u70b9\u89e6\u63a7\u548c\u591a\u79cd\u8f93\u5165\u8bbe\u5907\u3001\u62e5\u6709\u4e30\u5bcc\u7684\u7528\u6237\u754c\u9762\u63a7\u4ef6<\/strong>\uff0c\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Kivy\u7f16\u5199\u5b89\u5353\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001KIVY\u7b80\u4ecb<\/strong><\/h2>\n<p><p>Kivy \u662f\u4e00\u4e2a\u5f00\u6e90\u7684Python\u5e93\uff0c\u7528\u4e8e\u5feb\u901f\u5f00\u53d1\u591a\u70b9\u89e6\u63a7\u5e94\u7528\u7a0b\u5e8f\u3002\u5b83\u652f\u6301\u8de8\u5e73\u53f0\u5f00\u53d1\uff0c\u80fd\u591f\u5728Windows\u3001macOS\u3001Linux\u3001iOS\u548cAndroid\u4e0a\u8fd0\u884c\u3002Kivy\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u63a7\u4ef6\u548c\u5e03\u5c40\u7ba1\u7406\u5668\uff0c\u4f7f\u5f97\u5f00\u53d1\u8005\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u590d\u6742\u7684\u7528\u6237\u754c\u9762\u3002<\/p>\n<\/p>\n<p><h2>1.1 Kivy\u7684\u7279\u70b9<\/h2>\n<\/p>\n<p><p>Kivy\u5177\u6709\u4ee5\u4e0b\u51e0\u4e2a\u663e\u8457\u7279\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u8de8\u5e73\u53f0\u652f\u6301<\/strong>\uff1a\u4e00\u6b21\u7f16\u5199\uff0c\u968f\u5904\u8fd0\u884c\u3002Kivy\u652f\u6301Windows\u3001macOS\u3001Linux\u3001iOS\u548cAndroid\u7b49\u591a\u4e2a\u5e73\u53f0\u3002<\/li>\n<li><strong>\u591a\u70b9\u89e6\u63a7<\/strong>\uff1aKivy\u5185\u7f6e\u5bf9\u591a\u70b9\u89e6\u63a7\u7684\u652f\u6301\uff0c\u53ef\u4ee5\u5904\u7406\u591a\u79cd\u89e6\u63a7\u624b\u52bf\u3002<\/li>\n<li><strong>\u4e30\u5bcc\u7684\u63a7\u4ef6<\/strong>\uff1aKivy\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7528\u6237\u754c\u9762\u63a7\u4ef6\uff0c\u5982\u6309\u94ae\u3001\u6587\u672c\u8f93\u5165\u6846\u3001\u6807\u7b7e\u3001\u6ed1\u52a8\u6761\u7b49\u3002<\/li>\n<li><strong>\u9ad8\u6548\u7684\u56fe\u5f62\u5f15\u64ce<\/strong>\uff1aKivy\u4f7f\u7528OpenGL ES 2\u8fdb\u884c\u6e32\u67d3\uff0c\u80fd\u591f\u63d0\u4f9b\u9ad8\u6548\u7684\u56fe\u5f62\u5904\u7406\u80fd\u529b\u3002<\/li>\n<li><strong>\u6613\u4e8e\u5b66\u4e60<\/strong>\uff1aKivy\u7684API\u8bbe\u8ba1\u7b80\u6d01\u660e\u4e86\uff0c\u6613\u4e8e\u5b66\u4e60\u548c\u4f7f\u7528\u3002<\/li>\n<\/ul>\n<p><h2>1.2 \u5b89\u88c5Kivy<\/h2>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4f7f\u7528Kivy\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5148\u8fdb\u884c\u5b89\u88c5\u3002Kivy\u53ef\u4ee5\u901a\u8fc7pip\u8fdb\u884c\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install kivy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u5916\uff0c\u5982\u679c\u4f60\u9700\u8981\u5728Android\u4e0a\u8fd0\u884cKivy\u5e94\u7528\u7a0b\u5e8f\uff0c\u8fd8\u9700\u8981\u5b89\u88c5\u4e00\u4e9b\u989d\u5916\u7684\u5de5\u5177\uff0c\u5982Buildozer\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install buildozer<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e8c\u3001\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684Kivy\u5e94\u7528<\/strong><\/h2>\n<p><h2>2.1 \u521b\u5efaKivy\u5e94\u7528\u7684\u57fa\u672c\u7ed3\u6784<\/h2>\n<\/p>\n<p><p>\u4e00\u4e2aKivy\u5e94\u7528\u7a0b\u5e8f\u7684\u57fa\u672c\u7ed3\u6784\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\u90e8\u5206\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>App\u7c7b<\/strong>\uff1a\u8fd9\u662fKivy\u5e94\u7528\u7684\u4e3b\u7c7b\uff0c\u8d1f\u8d23\u7ba1\u7406\u5e94\u7528\u7684\u751f\u547d\u5468\u671f\u3002<\/li>\n<li><strong>Widget\u7c7b<\/strong>\uff1a\u8fd9\u662fKivy\u5e94\u7528\u7684\u57fa\u672cUI\u7ec4\u4ef6\uff0c\u53ef\u4ee5\u662f\u6309\u94ae\u3001\u6807\u7b7e\u7b49\u3002<\/li>\n<li><strong>\u5e03\u5c40\u7c7b<\/strong>\uff1a\u8d1f\u8d23\u7ba1\u7406UI\u7ec4\u4ef6\u7684\u5e03\u5c40\uff0c\u5982BoxLayout\u3001GridLayout\u7b49\u3002<\/li>\n<\/ul>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684Kivy\u5e94\u7528\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from kivy.app import App<\/p>\n<p>from kivy.uix.label import Label<\/p>\n<p>class MyApp(App):<\/p>\n<p>    def build(self):<\/p>\n<p>        return Label(text=&quot;Hello, Kivy!&quot;)<\/p>\n<p>if __name__ == &quot;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&quot;:<\/p>\n<p>    MyApp().run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2.2 \u8fd0\u884cKivy\u5e94\u7528<\/h2>\n<\/p>\n<p><p>\u4fdd\u5b58\u4e0a\u8ff0\u4ee3\u7801\u4e3a<code>main.py<\/code>\uff0c\u7136\u540e\u5728\u7ec8\u7aef\u4e2d\u8fd0\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python main.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f60\u5c06\u770b\u5230\u4e00\u4e2a\u7a97\u53e3\uff0c\u663e\u793a\u201cHello, Kivy!\u201d\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001\u5728Kivy\u4e2d\u4f7f\u7528\u5e03\u5c40<\/strong><\/h2>\n<p><p>Kivy\u63d0\u4f9b\u4e86\u591a\u79cd\u5e03\u5c40\u7c7b\uff0c\u7528\u4e8e\u7ba1\u7406UI\u7ec4\u4ef6\u7684\u5e03\u5c40\u3002\u5e38\u7528\u7684\u5e03\u5c40\u7c7b\u5305\u62ecBoxLayout\u3001GridLayout\u3001AnchorLayout\u7b49\u3002<\/p>\n<\/p>\n<p><h2>3.1 BoxLayout<\/h2>\n<\/p>\n<p><p>BoxLayout\u662f\u4e00\u79cd\u7b80\u5355\u7684\u5e03\u5c40\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5c06\u5b50\u7ec4\u4ef6\u6309\u6c34\u5e73\u6216\u5782\u76f4\u65b9\u5411\u6392\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from kivy.app import App<\/p>\n<p>from kivy.uix.boxlayout import BoxLayout<\/p>\n<p>from kivy.uix.button import Button<\/p>\n<p>class MyApp(App):<\/p>\n<p>    def build(self):<\/p>\n<p>        layout = BoxLayout(orientation=&#39;vertical&#39;)<\/p>\n<p>        layout.add_widget(Button(text=&#39;Button 1&#39;))<\/p>\n<p>        layout.add_widget(Button(text=&#39;Button 2&#39;))<\/p>\n<p>        layout.add_widget(Button(text=&#39;Button 3&#39;))<\/p>\n<p>        return layout<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    MyApp().run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3.2 GridLayout<\/h2>\n<\/p>\n<p><p>GridLayout\u53ef\u4ee5\u5c06\u5b50\u7ec4\u4ef6\u6309\u7f51\u683c\u6392\u5217\uff0c\u6307\u5b9a\u884c\u548c\u5217\u7684\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from kivy.app import App<\/p>\n<p>from kivy.uix.gridlayout import GridLayout<\/p>\n<p>from kivy.uix.button import Button<\/p>\n<p>class MyApp(App):<\/p>\n<p>    def build(self):<\/p>\n<p>        layout = GridLayout(cols=2)<\/p>\n<p>        layout.add_widget(Button(text=&#39;Button 1&#39;))<\/p>\n<p>        layout.add_widget(Button(text=&#39;Button 2&#39;))<\/p>\n<p>        layout.add_widget(Button(text=&#39;Button 3&#39;))<\/p>\n<p>        layout.add_widget(Button(text=&#39;Button 4&#39;))<\/p>\n<p>        return layout<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    MyApp().run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u56db\u3001\u5728Kivy\u4e2d\u4f7f\u7528\u4e8b\u4ef6\u5904\u7406<\/strong><\/h2>\n<p><p>Kivy\u652f\u6301\u591a\u79cd\u4e8b\u4ef6\u5904\u7406\u673a\u5236\uff0c\u5305\u62ec\u89e6\u6478\u4e8b\u4ef6\u3001\u952e\u76d8\u4e8b\u4ef6\u7b49\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7ed1\u5b9a\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u6765\u5904\u7406\u8fd9\u4e9b\u4e8b\u4ef6\u3002<\/p>\n<\/p>\n<p><h2>4.1 \u89e6\u6478\u4e8b\u4ef6<\/h2>\n<\/p>\n<p><p>Kivy\u4e2d\u7684\u6240\u6709Widget\u90fd\u53ef\u4ee5\u5904\u7406\u89e6\u6478\u4e8b\u4ef6\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7ed1\u5b9a<code>on_touch_down<\/code>\u3001<code>on_touch_move<\/code>\u548c<code>on_touch_up<\/code>\u65b9\u6cd5\u6765\u5904\u7406\u89e6\u6478\u4e8b\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from kivy.app import App<\/p>\n<p>from kivy.uix.widget import Widget<\/p>\n<p>class MyWidget(Widget):<\/p>\n<p>    def on_touch_down(self, touch):<\/p>\n<p>        print(&quot;Touch down:&quot;, touch)<\/p>\n<p>        return super().on_touch_down(touch)<\/p>\n<p>    def on_touch_move(self, touch):<\/p>\n<p>        print(&quot;Touch move:&quot;, touch)<\/p>\n<p>        return super().on_touch_move(touch)<\/p>\n<p>    def on_touch_up(self, touch):<\/p>\n<p>        print(&quot;Touch up:&quot;, touch)<\/p>\n<p>        return super().on_touch_up(touch)<\/p>\n<p>class MyApp(App):<\/p>\n<p>    def build(self):<\/p>\n<p>        return MyWidget()<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    MyApp().run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>4.2 \u952e\u76d8\u4e8b\u4ef6<\/h2>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7ed1\u5b9a<code>on_key_down<\/code>\u548c<code>on_key_up<\/code>\u65b9\u6cd5\u6765\u5904\u7406\u952e\u76d8\u4e8b\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from kivy.app import App<\/p>\n<p>from kivy.uix.widget import Widget<\/p>\n<p>from kivy.core.window import Window<\/p>\n<p>class MyWidget(Widget):<\/p>\n<p>    def __init__(self, kwargs):<\/p>\n<p>        super().__init__(kwargs)<\/p>\n<p>        Window.bind(on_key_down=self.on_key_down)<\/p>\n<p>        Window.bind(on_key_up=self.on_key_up)<\/p>\n<p>    def on_key_down(self, window, key, scancode, codepoint, modifier):<\/p>\n<p>        print(&quot;Key down:&quot;, key)<\/p>\n<p>        return True<\/p>\n<p>    def on_key_up(self, window, key, scancode):<\/p>\n<p>        print(&quot;Key up:&quot;, key)<\/p>\n<p>        return True<\/p>\n<p>class MyApp(App):<\/p>\n<p>    def build(self):<\/p>\n<p>        return MyWidget()<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    MyApp().run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e94\u3001Kivy\u4e2d\u7684\u52a8\u753b\u548c\u6548\u679c<\/strong><\/h2>\n<p><p>Kivy\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u7684\u52a8\u753b\u548c\u6548\u679c\uff0c\u53ef\u4ee5\u7528\u4e8e\u5236\u4f5c\u66f4\u52a0\u751f\u52a8\u7684\u7528\u6237\u754c\u9762\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>Animation<\/code>\u7c7b\u6765\u521b\u5efa\u548c\u7ba1\u7406\u52a8\u753b\u3002<\/p>\n<\/p>\n<p><h2>5.1 \u521b\u5efa\u7b80\u5355\u52a8\u753b<\/h2>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u52a8\u753b\u793a\u4f8b\uff0c\u5c06\u4e00\u4e2a\u6309\u94ae\u4ece\u5de6\u4fa7\u79fb\u52a8\u5230\u53f3\u4fa7\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from kivy.app import App<\/p>\n<p>from kivy.uix.button import Button<\/p>\n<p>from kivy.animation import Animation<\/p>\n<p>class MyApp(App):<\/p>\n<p>    def build(self):<\/p>\n<p>        button = Button(text=&quot;Animate me!&quot;)<\/p>\n<p>        button.bind(on_press=self.animate)<\/p>\n<p>        return button<\/p>\n<p>    def animate(self, instance):<\/p>\n<p>        animation = Animation(x=300, duration=2)<\/p>\n<p>        animation.start(instance)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    MyApp().run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>5.2 \u7ec4\u5408\u52a8\u753b<\/h2>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7ec4\u5408\u591a\u4e2a\u52a8\u753b\u6765\u521b\u5efa\u590d\u6742\u7684\u52a8\u753b\u6548\u679c\u3002\u4f8b\u5982\uff0c\u5c06\u4e00\u4e2a\u6309\u94ae\u4ece\u5de6\u4fa7\u79fb\u52a8\u5230\u53f3\u4fa7\uff0c\u540c\u65f6\u6539\u53d8\u5176\u5927\u5c0f\u548c\u989c\u8272\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from kivy.app import App<\/p>\n<p>from kivy.uix.button import Button<\/p>\n<p>from kivy.animation import Animation<\/p>\n<p>class MyApp(App):<\/p>\n<p>    def build(self):<\/p>\n<p>        button = Button(text=&quot;Animate me!&quot;)<\/p>\n<p>        button.bind(on_press=self.animate)<\/p>\n<p>        return button<\/p>\n<p>    def animate(self, instance):<\/p>\n<p>        animation = Animation(x=300, size=(200, 200), duration=2) + Animation(color=(1, 0, 0, 1), duration=1)<\/p>\n<p>        animation.start(instance)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    MyApp().run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u516d\u3001Kivy\u4e2d\u7684\u6587\u4ef6\u64cd\u4f5c<\/strong><\/h2>\n<p><p>Kivy\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u7684\u6587\u4ef6\u64cd\u4f5c\u529f\u80fd\uff0c\u5982\u8bfb\u53d6\u548c\u5199\u5165\u6587\u4ef6\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>FileChooser<\/code>\u63a7\u4ef6\u6765\u5b9e\u73b0\u6587\u4ef6\u9009\u62e9\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h2>6.1 \u8bfb\u53d6\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u6587\u4ef6\u8bfb\u53d6\u793a\u4f8b\uff0c\u8bfb\u53d6\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\u5e76\u663e\u793a\u5176\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from kivy.app import App<\/p>\n<p>from kivy.uix.boxlayout import BoxLayout<\/p>\n<p>from kivy.uix.button import Button<\/p>\n<p>from kivy.uix.label import Label<\/p>\n<p>from kivy.uix.filechooser import FileChooser<\/p>\n<p>class MyApp(App):<\/p>\n<p>    def build(self):<\/p>\n<p>        layout = BoxLayout(orientation=&#39;vertical&#39;)<\/p>\n<p>        self.label = Label(text=&quot;No file selected&quot;)<\/p>\n<p>        button = Button(text=&quot;Select file&quot;)<\/p>\n<p>        button.bind(on_press=self.select_file)<\/p>\n<p>        layout.add_widget(self.label)<\/p>\n<p>        layout.add_widget(button)<\/p>\n<p>        return layout<\/p>\n<p>    def select_file(self, instance):<\/p>\n<p>        filechooser = FileChooser()<\/p>\n<p>        filechooser.bind(on_selection=self.load_file)<\/p>\n<p>        self.root.add_widget(filechooser)<\/p>\n<p>    def load_file(self, filechooser, selection):<\/p>\n<p>        with open(selection[0], &#39;r&#39;) as file:<\/p>\n<p>            self.label.text = file.read()<\/p>\n<p>        self.root.remove_widget(filechooser)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    MyApp().run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>6.2 \u5199\u5165\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u6587\u4ef6\u5199\u5165\u793a\u4f8b\uff0c\u5c06\u7528\u6237\u8f93\u5165\u7684\u6587\u672c\u5199\u5165\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from kivy.app import App<\/p>\n<p>from kivy.uix.boxlayout import BoxLayout<\/p>\n<p>from kivy.uix.button import Button<\/p>\n<p>from kivy.uix.textinput import TextInput<\/p>\n<p>from kivy.uix.filechooser import FileChooser<\/p>\n<p>class MyApp(App):<\/p>\n<p>    def build(self):<\/p>\n<p>        layout = BoxLayout(orientation=&#39;vertical&#39;)<\/p>\n<p>        self.textinput = TextInput(hint_text=&quot;Enter some text&quot;)<\/p>\n<p>        button = Button(text=&quot;Save to file&quot;)<\/p>\n<p>        button.bind(on_press=self.save_file)<\/p>\n<p>        layout.add_widget(self.textinput)<\/p>\n<p>        layout.add_widget(button)<\/p>\n<p>        return layout<\/p>\n<p>    def save_file(self, instance):<\/p>\n<p>        filechooser = FileChooser()<\/p>\n<p>        filechooser.bind(on_selection=self.write_file)<\/p>\n<p>        self.root.add_widget(filechooser)<\/p>\n<p>    def write_file(self, filechooser, selection):<\/p>\n<p>        with open(selection[0], &#39;w&#39;) as file:<\/p>\n<p>            file.write(self.textinput.text)<\/p>\n<p>        self.root.remove_widget(filechooser)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    MyApp().run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e03\u3001\u5728Android\u4e0a\u90e8\u7f72Kivy\u5e94\u7528<\/strong><\/h2>\n<p><p>\u4e3a\u4e86\u5728Android\u4e0a\u90e8\u7f72Kivy\u5e94\u7528\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528Buildozer\uff0c\u8fd9\u662f\u4e00\u4e2a\u81ea\u52a8\u5316\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5c06Kivy\u5e94\u7528\u6253\u5305\u4e3aAndroid APK\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h2>7.1 \u5b89\u88c5Buildozer<\/h2>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5Buildozer\u3002\u53ef\u4ee5\u901a\u8fc7pip\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install buildozer<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u5916\uff0c\u8fd8\u9700\u8981\u5b89\u88c5\u4e00\u4e9b\u989d\u5916\u7684\u4f9d\u8d56\u9879\uff0c\u5982Android SDK\u3001NDK\u548cCython\u3002\u53ef\u4ee5\u53c2\u8003Buildozer\u7684\u5b98\u65b9\u6587\u6863\u8fdb\u884c\u8be6\u7ec6\u7684\u5b89\u88c5\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h2>7.2 \u521b\u5efaBuildozer\u914d\u7f6e\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>\u5728\u9879\u76ee\u76ee\u5f55\u4e0b\uff0c\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u751f\u6210Buildozer\u914d\u7f6e\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">buildozer init<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u5728\u9879\u76ee\u76ee\u5f55\u4e0b\u751f\u6210\u4e00\u4e2a<code>buildozer.spec<\/code>\u6587\u4ef6\u3002\u4f60\u53ef\u4ee5\u7f16\u8f91\u8fd9\u4e2a\u6587\u4ef6\u6765\u914d\u7f6e\u6253\u5305\u9009\u9879\uff0c\u5982\u5e94\u7528\u540d\u79f0\u3001\u5305\u540d\u3001\u56fe\u6807\u7b49\u3002<\/p>\n<\/p>\n<p><h2>7.3 \u6253\u5305\u5e94\u7528<\/h2>\n<\/p>\n<p><p>\u5728\u914d\u7f6e\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u6253\u5305\u5e94\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">buildozer -v android debug<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u751f\u6210\u4e00\u4e2aAPK\u6587\u4ef6\uff0c\u53ef\u4ee5\u5b89\u88c5\u5230Android\u8bbe\u5907\u4e0a\u8fdb\u884c\u6d4b\u8bd5\u3002<\/p>\n<\/p>\n<h2><strong>\u516b\u3001\u603b\u7ed3<\/strong><\/h2>\n<p><p>Kivy\u662f\u4e00\u4e2a\u5f3a\u5927\u4e14\u6613\u4e8e\u4f7f\u7528\u7684Python\u5e93\uff0c\u53ef\u4ee5\u7528\u4e8e\u5feb\u901f\u5f00\u53d1\u8de8\u5e73\u53f0\u7684\u79fb\u52a8\u5e94\u7528\u3002\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u5b66\u4e60\u4e86\u5982\u4f55\u4f7f\u7528Kivy\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Android\u5e94\u7528\uff0c\u5305\u62ec\u5e03\u5c40\u7ba1\u7406\u3001\u4e8b\u4ef6\u5904\u7406\u3001\u52a8\u753b\u6548\u679c\u548c\u6587\u4ef6\u64cd\u4f5c\u7b49\u5185\u5bb9\u3002\u6700\u540e\uff0c\u6211\u4eec\u8fd8\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528Buildozer\u5c06Kivy\u5e94\u7528\u6253\u5305\u4e3aAndroid APK\u6587\u4ef6\u3002\u5e0c\u671b\u672c\u6587\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u638c\u63e1Kivy\u7684\u4f7f\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5f00\u53d1\u5b89\u5353\u5e94\u7528\uff1f<\/strong><br \/>Python\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e9b\u6846\u67b6\u548c\u5de5\u5177\u6765\u5f00\u53d1\u5b89\u5353\u5e94\u7528\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528Kivy\u3001BeeWare\u548cPyQt\u7b49\u6846\u67b6\u3002Kivy\u662f\u4e00\u4e2a\u6d41\u884c\u7684\u5f00\u6e90Python\u5e93\uff0c\u9002\u5408\u5feb\u901f\u5f00\u53d1\u591a\u5e73\u53f0\u5e94\u7528\u3002BeeWare\u5141\u8bb8\u4f60\u4f7f\u7528Python\u7f16\u5199\u539f\u751f\u5e94\u7528\uff0c\u800cPyQt\u5219\u9002\u5408\u9700\u8981\u4e30\u5bcc\u7528\u6237\u754c\u9762\u7684\u5e94\u7528\u5f00\u53d1\u3002\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u53ef\u5e2e\u52a9\u7b80\u5316\u5f00\u53d1\u6d41\u7a0b\u3002<\/p>\n<p><strong>Python\u5f00\u53d1\u5b89\u5353\u5e94\u7528\u65f6\u9700\u8981\u54ea\u4e9b\u73af\u5883\u914d\u7f6e\uff1f<\/strong><br \/>\u5728\u5f00\u59cb\u5f00\u53d1\u4e4b\u524d\uff0c\u786e\u4fdd\u5b89\u88c5Python\u53ca\u76f8\u5173\u5e93\u3002\u5bf9\u4e8eKivy\uff0c\u9700\u8981\u5b89\u88c5Kivy\u6846\u67b6\u548cBuildozer\u5de5\u5177\uff0c\u8fd9\u5c06\u5e2e\u52a9\u4f60\u5c06\u5e94\u7528\u6253\u5305\u4e3aAPK\u683c\u5f0f\u3002\u5728Windows\u3001Linux\u6216MacOS\u4e0a\u90fd\u53ef\u4ee5\u8fdb\u884c\u73af\u5883\u914d\u7f6e\uff0c\u786e\u4fdd\u4f60\u7684\u5f00\u53d1\u73af\u5883\u4e0e\u76ee\u6807\u5b89\u5353\u8bbe\u5907\u517c\u5bb9\u3002<\/p>\n<p><strong>\u5982\u4f55\u6d4b\u8bd5\u548c\u8c03\u8bd5\u7528Python\u5f00\u53d1\u7684\u5b89\u5353\u5e94\u7528\uff1f<\/strong><br \/>\u6d4b\u8bd5\u548c\u8c03\u8bd5Python\u5f00\u53d1\u7684\u5b89\u5353\u5e94\u7528\u53ef\u4ee5\u901a\u8fc7Android\u6a21\u62df\u5668\u6216\u771f\u5b9e\u8bbe\u5907\u8fdb\u884c\u3002\u4f7f\u7528ADB\uff08Android Debug Bridge\uff09\u5de5\u5177\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u8fdb\u884c\u8c03\u8bd5\u3002Kivy\u548c\u5176\u4ed6\u6846\u67b6\u901a\u5e38\u4f1a\u63d0\u4f9b\u4e00\u4e9b\u8c03\u8bd5\u5de5\u5177\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u5feb\u901f\u53d1\u73b0\u548c\u4fee\u590d\u95ee\u9898\u3002\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u4fdd\u6301\u4ee3\u7801\u7ed3\u6784\u6e05\u6670\uff0c\u4f7f\u7528\u65e5\u5fd7\u8bb0\u5f55\u529f\u80fd\uff0c\u6709\u52a9\u4e8e\u66f4\u597d\u5730\u5b9a\u4f4dbug\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u7f16\u5199\u5b89\u5353\u7a0b\u5e8f\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528Kivy\u3001BeeWare\u3001Pyqtdeploy\u548cSL4A\u7b49\u3002\u672c\u6587 [&hellip;]","protected":false},"author":3,"featured_media":1186115,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1186108"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1186108"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1186108\/revisions"}],"predecessor-version":[{"id":1186116,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1186108\/revisions\/1186116"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1186115"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1186108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1186108"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1186108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}