Ali Kheyrandish
Ali Kheyrandish

Reputation: 15

error in .kv file==> File "/home/pi/Desktop/ali/my9.kv"

My code lights up an LED. This program works in Tkinter but it did not work when I wrote it with Kivy!! what is the problem?

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()

my.kv==>

WindowManager:
    MainWindow:

<MainWindow>:
    name: "main"

    GridLayout:
        cols:1

        Button:
            text: "Submit"
                TheRequest = requests.get('http://192.168.43.91/on')

error ==> File "/home/pi/Desktop/ali/my9.kv", line 13, in TheRequest = requests.get('http://192.168.43.91/on') NameError: name 'requests' is not defined

Upvotes: 1

Views: 39

Answers (1)

amras
amras

Reputation: 1599

You need to import the requests module in kv file. And also add the TheRequest = requests.get('http://192.168.43.91/on') statement under on_release method of the Button. The modified kv file would be like below:

#:import requests requests
WindowManager:
    MainWindow:

<MainWindow>:
    name: "main"

    GridLayout:
        cols:1

        Button:
            text: "Submit"
            on_release:
                TheRequest = requests.get('https://www.android.com/')

Upvotes: 1

Related Questions