Alex_P
Alex_P

Reputation: 2952

Python/Kivy crashes instantly

Overall question is: Why does my Kivy application crashes instantly?

My Python code is:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.widget import Widget
from kivy.graphics import Line

class MainScreen(Screen):
    pass

class AnotherScreen(Screen):
    pass

class ScreenManagement(ScreenManager):
    pass

class TouchInput(Widget):
    def on_touch_down(self, touch):
        with self.canvas:
            touch.ud["line"] = Line(points=(touch.x, touch.y))
    def on_touch_move(self, touch):
        touch.ud["line"].points += (touch.x, touch.y)

presentation = Builder.load_file("simplekivy.kv")

class SimpleKivy(App):
    def build(self):
        return presentation

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

My Kv. code is:

#: import FadeTransition kivy.uix.screenmanager.FadeTransition

ScreenManagement:
    transition: FadeTransition()
    MainScreen:
    AnotherScreen:

<MainScreen>:
    name: "Main"
    Button:
        on_release: app.root.current = "Other"
        text: "Next screen!"
        font_size: 50

<AnotherScreen>:
    name: "Other"
    FloatLayout:
        TouchInput:
            id: touch
        Button:
            on_release: app.root.current = "Main"
            text: "Going back!"
            font_size: 40
            color: 0,1,0,1
            size_hint: 0.3, 0.2
            pos_hint: {'right': 1, 'top': 1}

        Button:
            on_release: touch.canvas.clear()
            text: "Clear window"
            font_size: 40
            color: 0,1,0,1
            size_hint: 0.3, 0.2
            pos_hint: {'right': 1, 'top': 0}

When I would remove the 'Clear window'-Button, the application works as expected. However, the moment I add the button, it crashes instantly with the error message:

Python has stopped working

Upvotes: 0

Views: 589

Answers (1)

Haga Kenew
Haga Kenew

Reputation: 41

when you make a code in python and use kv the library itself will look for the .kv file in the same directory where the .py file is, so you do not have to start through 'presentation = Builder.load_file (" simplekivy. kv ")'

this can be solved changing the python code to:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.widget import Widget
from kivy.graphics import Line

class MainScreen(Screen):
    pass

class AnotherScreen(Screen):
    pass

class ScreenManagement(ScreenManager):
    pass

class TouchInput(Widget):
    def on_touch_down(self, touch):
        with self.canvas:
            touch.ud["line"] = Line(points=(touch.x, touch.y))
    def on_touch_move(self, touch):
        touch.ud["line"].points += (touch.x, touch.y)
#don't need this
#presentation = Builder.load_file("simplekivy.kv")

class SimpleKivy(App):
    pass
    #I commented on that part of your code and added a 'pass' above
    #def build(self):
    #    return presentation

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

Upvotes: 1

Related Questions