Black screen when running a program with Kivy

1

I do not understand why when I run the following code the screen appears only black. Can someone give me a hand?

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()
    
asked by anonymous 04.10.2018 / 17:54

1 answer

2

As commented by Anderson python is case-sensitive (case-sensitive), and according to the kivy documentation the build() method is in the lowercase (lower case). To correct it, you only have to replace Build() with build()

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

You have other errors in your code:

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

They have spaces between __ and name before and after, so does __ main __ . The correct would be without the spaces getting like this:

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

Error 2): In the build() method 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) .

The correct one:

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 create layouts in a separate .kv file, leave the classes to create the "business rules".

    
05.10.2018 / 18:35