应用程序名称在kivy中

时间:2017-09-12 18:58:07

标签: kivy kivy-language

我有两个带有一个kv文件的应用程序。两个应用程序之间仅在app类名称中有所不同。应用A给出了良好的结果,但应用B是坏的。哪里有问题?

申请A:

import kivy
kivy.require('1.0.5')
from kivy.lang.builder import Builder

from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.app import App

class MyW(GridLayout):
    pass

class ShowApp(App):
def build(self):
    Builder.load_file('d:\\MyPgm\\Python\\kivy\\ControlShow    \\ControlShow.kv')
    return MyW()


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

申请B:

import kivy
kivy.require('1.0.5')
from kivy.lang.builder import Builder

from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.app import App

class MyW(GridLayout):
    pass

class ControlShowApp(App):
def build(self):
    Builder.load_file('d:\\MyPgm\\Python\\kivy\\ControlShow    \\ControlShow.kv')
    return MyW()


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

KV档案:

<MyW>
    cols: 2
    rows: 2
    Button:
      id: label1
      text: 'B1'
    Button:
      id: label2
      text: 'B2'
    Button:
      id: label3
      text: 'B3'
    Button:
      id: label4
      text: 'B4'

1 个答案:

答案 0 :(得分:1)

问题如下:

  1. 一个类规则,由&lt;之间的widget类的名称声明。 &GT;然后是:例如&LT; MYW&GT;:
  2. 缩进是4个空格
  3. 实施例

    ControlShow.kv

    #:kivy 1.10.0
    
    <MyW>:
        cols: 2
        rows: 2
        Button:
            id: label1
            text: 'B1'
        Button:
            id: label2
            text: 'B2'
        Button:
            id: label3
            text: 'B3'
        Button:
            id: label4
            text: 'B4'
    

    ShowApp.py

    from kivy.lang.builder import Builder
    from kivy.uix.gridlayout import GridLayout
    from kivy.app import App
    
    
    class MyW(GridLayout):
        pass
    
    
    class ShowApp(App):
        def build(self):
            Builder.load_file('d:\\MyPgm\\Python\\kivy\\ControlShow    \\ControlShow.kv')
            return MyW()
    
    
    if __name__ == '__main__':
        ShowApp().run()
    

    ControlShowApp.py

    from kivy.lang.builder import Builder
    from kivy.uix.gridlayout import GridLayout
    from kivy.app import App
    
    
    class MyW(GridLayout):
        pass
    
    
    class ControlShowApp(App):
        def build(self):
            Builder.load_file('d:\\MyPgm\\Python\\kivy\\ControlShow    \\ControlShow.kv')
            return MyW()
    
    
    if __name__ == '__main__':
        ControlShowApp().run()
    

    输出

    enter image description here