Black screen when running a program with Kivy

Asked

Viewed 488 times

1

I’m not understanding why when I run the following code the screen appears only black. Someone can give me a help?

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.image import Image

class Principal(App):
    def Build(self):
        self.img1 = Image(source= "images/Logo.jpg",size_hint= (.5, .3),pos_hint={"center_x":.5, "center_y":.9}),
        self.img2 = Image(source= "images/super.png",size_hint= (.99, .3), pos_hint={"center_x":.5, "center_y":.65}),
        lb1 = Label(text="Usuário: ",font_size= '15sp',size_hint= (None,None),pos_hint= {"center_x":.40, "center_y":.43},bold= True),
        lb2 = Label(text="Senha: ",size_hint= (None,None),font_size= "15sp",pos_hint={"center_x":.40, "center_y":.36},bold= True),
        lb3 = Label(text="Cadatre-se!",size_hint= (None, None),font_size= "8sp",pos_hint= {"center_x":.5, "center_y":.3},bold= True),
        lb4 = Label(text="Esqueceu sua senha?",size_hint= (None, None),font_size= "8sp",pos_hint= {"center_x":.5, "center_y":.32},bold= True)

        layoutVideo = FloatLayout()

        layoutVideo.add_widget(self.img1)
        layoutVideo.add_widget(self.img2)
        layoutVideo.add_widget(lb1)
        layoutVideo.add_widget(lb2)
        layoutVideo.add_widget(lb3)
        layoutVideo.add_widget(lb4)


        return layoutVideo

if __ name __ == "__ main __":

      Principal().run()
  • 5

    Maybe because you created the method Build and Kivy is waiting build, with tiny b? Remember that Python is case-sensitive, then the two words are not equal.

1 answer

2


As commented by Anderson python is case-sensitive (case-sensitive), and according to the kivy documentation the method build() is in low (lower case). To correct just replace Build() for build()

Earlier wrong:

def Build(self):
        self.img1 = Image(source= "images/Logo.jpg",size_hint= (.5, .3),pos_hint={"center_x":.5, "center_y":.9}),
        self.img2 = Image(source= "images/super.png",size_hint= (.99, .3), pos_hint={"center_x":.5, "center_y":.65}),
        lb1 = Label(text="Usuário: ",font_size= '15sp',size_hint= (None,None),pos_hint= {"center_x":.40, "center_y":.43},bold= True),
        lb2 = Label(text="Senha: ",size_hint= (None,None),font_size= "15sp",pos_hint={"center_x":.40, "center_y":.36},bold= True),
        lb3 = Label(text="Cadatre-se!",size_hint= (None, None),font_size= "8sp",pos_hint= {"center_x":.5, "center_y":.3},bold= True),
        lb4 = Label(text="Esqueceu sua senha?",size_hint= (None, None),font_size= "8sp",pos_hint= {"center_x":.5, "center_y":.32},bold= True)

        layoutVideo = FloatLayout()

        layoutVideo.add_widget(self.img1)
        layoutVideo.add_widget(self.img2)
        layoutVideo.add_widget(lb1)
        layoutVideo.add_widget(lb2)
        layoutVideo.add_widget(lb3)
        layoutVideo.add_widget(lb4)


        return layoutVideo

Correct, changing Build() for build():

def build(self):
        self.img1 = Image(source= "images/Logo.jpg",size_hint= (.5, .3),pos_hint={"center_x":.5, "center_y":.9}),
        self.img2 = Image(source= "images/super.png",size_hint= (.99, .3), pos_hint={"center_x":.5, "center_y":.65}),
        lb1 = Label(text="Usuário: ",font_size= '15sp',size_hint= (None,None),pos_hint= {"center_x":.40, "center_y":.43},bold= True),
        lb2 = Label(text="Senha: ",size_hint= (None,None),font_size= "15sp",pos_hint={"center_x":.40, "center_y":.36},bold= True),
        lb3 = Label(text="Cadatre-se!",size_hint= (None, None),font_size= "8sp",pos_hint= {"center_x":.5, "center_y":.3},bold= True),
        lb4 = Label(text="Esqueceu sua senha?",size_hint= (None, None),font_size= "8sp",pos_hint= {"center_x":.5, "center_y":.32},bold= True)

        layoutVideo = FloatLayout()

        layoutVideo.add_widget(self.img1)
        layoutVideo.add_widget(self.img2)
        layoutVideo.add_widget(lb1)
        layoutVideo.add_widget(lb2)
        layoutVideo.add_widget(lb3)
        layoutVideo.add_widget(lb4)


        return layoutVideo

There are other errors in your code:

Error 1): if __ name __ == "__ main __": Principal().run()

Are with spaces between __ and name before and after, the same occurs in __ main __. The right thing would be without the spaces being like this:

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

Error 2): In the method build() at the end of each line has a comma, causing a tuple to be assigned to the variable containing the object and None, (<kivy.uix.label.Label object at 0x7ff7c5ae6660>,None).

Right:

def build(self):
        lb1 = Label(text="Usuário: ",font_size= '15sp',size_hint= (None,None),pos_hint= {"center_x":.40, "center_y":.43},bold= True)
        lb2 = Label(text="Senha: ",size_hint= (None,None),font_size= "15sp",pos_hint={"center_x":.40, "center_y":.36},bold= True)
        lb3 = Label(text="Cadatre-se!",size_hint= (None, None),font_size= "8sp",pos_hint= {"center_x":.5, "center_y":.3},bold= True)
        lb4 = Label(text="Esqueceu sua senha?",size_hint= (None, None),font_size= "8sp",pos_hint= {"center_x":.5, "center_y":.32},bold= True)
# o restante do codigo...

Tip: Give preference to creating layouts in a file .kv separate, leave the classes to create the "business rules".

  • Curiosity: you executed the code to know if this was the only problem?

  • Yes, except for the spaces in __ name __ and __ main __which I believe must have been a mistake during the issue of the question.

  • Thanks :D It is that I would test here, but I ended up not getting time. As for spaces, yes, I find it interesting to add in the answer. We may think it was just carelessness, but it may not. It doesn’t hurt to make it clear that you are wrong :D

  • They were in the original version too ^^

  • Moscada my :# , had even, editing the answer

Browser other questions tagged

You are not signed in. Login or sign up in order to post.