Home > Mobile >  Trying to Run Kivy Application on MacOS, but getting black screen when run
Trying to Run Kivy Application on MacOS, but getting black screen when run

Time:05-30

I have been trying to run my kivy application on my Mac, however when I run it all I get is a black screen. I have tried doing everything that I can, however I simply cannot get it to work. I have also tried manually loading the kv file, but that also did not work.

Here is my Python Code:

import kivy
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
import random


kivy.require('2.0.0') 

class MyRoot(BoxLayout):
    def __init__(self):
        super().__init__()

    def generate_number(self):
        self.random_label.text = str(random.randint(0, 1000))

class Test(App):

    def build(self):
        return MyRoot()

Test = Test()
Test.run()

Here is my KV File:

<MyRoot>:

    random_label: random_label
    
    BoxLayout:
        orientation: 'vertical'
        Label:
            text: 'Random'
            font_size: 64
            color: 0.92, 0.45, 0
        Label:
            id: random_label
            text: '-'
            font_size: 64

        Button:
            text: 'Generate'
            font_size: 32
            size: 100, 50
            on_press: root.generate_number()

CodePudding user response:

I am going to make a wild guess that your kivy file is not correctly named. According to the Kivy documentation,

By name convention:

Kivy looks for a Kv file with the same name as your App class in lowercase, minus “App” if it ends with ‘App’ e.g:

MyApp -> my.kv

I put the kivy script in the same directory as the python script, and named the kivy script test.kv. The file structure looks like this:

.
├── main.py
└── test.kv

It produces a window with some texts and buttons.

Tell me if it solves your problem.

CodePudding user response:

I found out that the issue was with the python version I ran it with Python 3.10 instead of 3.9 and now it worked.

  • Related