首页 > 解决方案 > 缩进无效,必须是8个空格的倍数,圆角按钮kivy .kv文件

问题描述

所以是的,我正在学习 kivy - “圆形按钮”,当我运行教程的程序时 --------------- 错误: https ://i.stack.imgur.com/rGhSa.png Python:

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.core.window import Window
Builder.load_file("my.kv")
class MyLayout(Widget,App):
    def __init__(self,*args,**kwargs):
        super(MyLayout, self).__init__(**kwargs)

class AwesomeApp(App):
    def build(self):
        Window.clearcolor = (1,1,1,1)
        return MyLayout()
if __name__ == '__main__':
    AwesomeApp().run()

.kv

<MyLayout>
    BoxLayout:
            orientation: "vertical"
            size: root.width, root.height
            padding: 50
            spacing: 20
            Button:
                text: "Hello World!"
            RoundedButton:
                text: "Goodbye World!"
                pos_hint: {'center_x': 0.5}
                size_hint: (1, .3)
                #background_color: (0/255,255/255,203/255,1)
                #background_normal: ''
<RoundedButton@Button>
    background_color: (0,0,0,0)
    background_normal: ''
    canvas.before:
        Color:
            rgba: (0/255,255/255,203/255,1)
        RoundedRectangle:
            size: self.size
            pos: self.pos
            radius: [58]

谢谢,谁能帮忙,不喜欢这些错误,缩进错误看起来像

标签: kivykivy-languagepython-3.8

解决方案


您没有进行正确的缩进,您必须在文件中仅使用一种类型的缩进,但是您使用 8 个空格和 4 个空格作为缩进,这就是错误出现的原因。

在这里,我在整个代码中使用了 4 个空格作为缩进,这就是它工作正常的原因。

<MyLayout>
    BoxLayout:
        orientation: "vertical"
        size: root.width, root.height
        padding: 50
        spacing: 20
        Button:
            text: "Hello World!"
        RoundedButton:
            text: "Goodbye World!"
            pos_hint: {'center_x': 0.5}
            size_hint: (1, .3)
                #background_color: (0/255,255/255,203/255,1)
                #background_normal: ''
<RoundedButton@Button>
    background_color: (0,0,0,0)
    background_normal: ''
    canvas.before:
        Color:
            rgba: (0/255,255/255,203/255,1)
        RoundedRectangle:
            size: self.size
            pos: self.pos
            radius: [58]

推荐阅读