Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

can two same pieces of code give different results?

my first day of learning kivy framework .

the kivy website shows :

import kivy
kivy.require('2.1.0') # replace with your current kivy version !

from kivy.app import App
from kivy.uix.label import Label


class MyApp(App):

    def build(self):
        return Label(text='Hello world')


if __name__ == '__main__':
    MyApp().run()

to create an application and it works as expected . followed manually and made a file named first.py and wrote :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import kivy
kivy.require('2.1.0')

from kivy.app import App
from kivy.uix.label import Label

class MyApp(App):
    
    def build(self):
        return Label(text="hello world")
    

if __name__ == "__main__":
    MyApp.run()

and this code is showing the error :

 d:/kivlearn/first.py
[INFO   ] [Logger      ] Record log in C:\Users\sahil\.kivy\logs\kivy_23-10-12_25.txt
[INFO   ] [deps        ] Successfully imported "kivy_deps.angle" 0.3.3
[INFO   ] [deps        ] Successfully imported "kivy_deps.glew" 0.3.1
[INFO   ] [deps        ] Successfully imported "kivy_deps.sdl2" 0.6.0
[INFO   ] [Kivy        ] v2.2.1
[INFO   ] [Kivy        ] Installed at "D:\kivlearn\kivy_venv\Lib\site-packages\kivy\__init__.py"
[INFO   ] [Python      ] v3.11.6 (tags/v3.11.6:8b6ee5b, Oct  2 2023, 14:57:12) [MSC v.1935 64 bit (AMD64)]
[INFO   ] [Python      ] Interpreter at "D:\kivlearn\kivy_venv\Scripts\python.exe"
[INFO   ] [Logger      ] Purge log fired. Processing...
[INFO   ] [Logger      ] Purge finished!
[INFO   ] [Factory     ] 190 symbols loaded
[INFO   ] [Image       ] Providers: img_tex, img_dds, img_sdl2, img_pil (img_ffpyplayer ignored)
[INFO   ] [Text        ] Provider: sdl2
 Traceback (most recent call last):
   File "d:\kivlearn\first.py", line 14, in <module>
     MyApp.run()
 TypeError: App.run() missing 1 required positional argument: 'self'
(kivy_venv) PS D:\kivlearn> 

is there a difference in both the pieces of code? what to do?

i tried switching back to global installation rather than the environment , but the problem persisted.

>Solution :

The two pieces of code are not the same, note in particular:

if __name__ == '__main__':
    MyApp().run()

vs.

if __name__ == "__main__":
    MyApp.run()

The first example instantiates MyApp, and then calls run, the second only calls the method on the class object. This also explains why you see the error message that you see (self doesn’t exist in the second context).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading