.kv 文件中的错误 ==> 文件“/home/pi/Desktop/ali/my9.kv”

我的代码点亮了一个 LED。这个程序在 Tkinter 中有效,但当我用 Kivy 编写它时它不起作用!!问题是什么?


kivy.py ==>


from kivy.app import App

from kivy.lang import Builder

from kivy.uix.screenmanager import ScreenManager, Screen

import requests


class MainWindow(Screen):

    pass



class WindowManager(ScreenManager):

    pass



kv = Builder.load_file("my9.kv")



class MyMainApp(App):

    def build(self):

        return kv



if __name__ == "__main__":

    MyMainApp().run()

我的.kv==>


WindowManager:

    MainWindow:


<MainWindow>:

    name: "main"


    GridLayout:

        cols:1


        Button:

            text: "Submit"

                TheRequest = requests.get('http://192.168.43.91/on')

错误 ==> 文件“/home/pi/Desktop/ali/my9.kv”,第 13 行,在 TheRequest = requests.get('http://192.168.43.91/on') NameError: name 'requests' is not定义的


开满天机
浏览 222回答 1
1回答

慕森卡

您需要kv 文件中的import模块requests。并且还在 的方法TheRequest = requests.get('http://192.168.43.91/on')下添加语句。修改后的 kv 文件如下所示:on_releaseButton#:import requests requestsWindowManager:&nbsp; &nbsp; MainWindow:<MainWindow>:&nbsp; &nbsp; name: "main"&nbsp; &nbsp; GridLayout:&nbsp; &nbsp; &nbsp; &nbsp; cols:1&nbsp; &nbsp; &nbsp; &nbsp; Button:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text: "Submit"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; on_release:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TheRequest = requests.get('https://www.android.com/')
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python