如何运行kivy程序?

时间:2016-08-06 13:13:20

标签: python kivy

我用python kivy lib编写了一些程序,但它没有用。

import kivy
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.graphics import Color

class Controller(BoxLayout):

    def welcome(self):
        wid = BoxLayout(orientation='vertical')
        wid.add_widget(Label(text='hellow',size_hint=(1,.1)))
        wid.add_widget(Button(text='welcom',size_hint=(1,.1)))
        wid.add_widget(Button(text='khoroj',size_hint=(1,.1)))
        wid.add_widget(Button(text='rahnama',size_hint=(1,.1)))

class UiApp(App):
    def build(self):   
        root = Controller()
        return root 
if __name__ == '__main__':
        UiApp().run()

当我运行程序时,只显示一个黑色窗口,没有窗口小部件。可能是什么问题?

1 个答案:

答案 0 :(得分:1)

这里有两个问题。

我将在这个答案中展示两个例子。第一个没有嵌套BoxLayout,第二个没有嵌套BoxLayout 在这两个示例中,我将使用__init__代替welcome()
您也可以使用welcome()。请阅读以下有关如何操作的信息。

然后是两个问题:

第一:

您永远不会在welcome()课程中运行Controller方法 在返回build之前,您可以通过在应用root方法中运行它来进行修复。像这样:

root.welcome()
return root

或者你可以把它放在课堂上的__init__方法中。在我解释这里的第二个问题之后,我将展示一个例子。

第二个问题是您在班级中创建了一个新的BoxLayout,它已经继承了BoxLayout。但是你永远不会将这个新BoxLayout添加到你的小部件,在这种情况下,allready是BoxLayout

那么,如何解决这个问题 由于该类已经继承了BoxLayout,因此您无需在这个简单的应用程序中创建新的。只有当您需要嵌套BoxLayout时,才会这样做。

假设您不需要嵌套另一个BoxLayout 有关如何执行此操作的示例,并使用__init__方法:

import kivy
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout


class Controller(BoxLayout):

    def __init__(self,**kwargs):
        super(Controller,self).__init__(**kwargs)
        # this is what you need to overite the BoxLayout's __init__ method

        # self.orientation and self.add_widget because self is the BoxLayout you inherited
        self.orientation='vertical'
        self.add_widget(Label(text='hellow',size_hint=(1,.1)))
        self.add_widget(Button(text='welcom',size_hint=(1,.1)))
        self.add_widget(Button(text='khoroj',size_hint=(1,.1)))
        self.add_widget(Button(text='rahnama',size_hint=(1,.1)))


class UiApp(App):
    def build(self):
        root = Controller()
        return root


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

让我们说你需要嵌套另一个BoxLayout 你会这样做:

import kivy
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout


class Controller(BoxLayout):

    def __init__(self,**kwargs):
        super(Controller,self).__init__(**kwargs)
        # this is what you need to overite the BoxLayout's __init__ method
        # I make this BoxLayout horizontal, and add a Button, just to show the idea of nesting
        self.orientation='horizontal'

        self.add_widget(Button(text='First Button'))


        self.nested_boxlayout = BoxLayout(orientation="vertical")

        # then we add stuff to the nested layout

        self.nested_boxlayout.add_widget(Label(text='hellow',size_hint=(1,.1)))
        self.nested_boxlayout.add_widget(Button(text='welcom',size_hint=(1,.1)))
        self.nested_boxlayout.add_widget(Button(text='khoroj',size_hint=(1,.1)))
        self.nested_boxlayout.add_widget(Button(text='rahnama',size_hint=(1,.1)))

        # but we still need to add the nested layout to the root layout. (nest it)

        self.add_widget(self.nested_boxlayout)


class UiApp(App):
    def build(self):
        root = Controller()
        return root


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