如何在 .kv 文件中使用网络浏览器

How to use webbrowser in .kv file

我正在尝试在我的 .kv 文件中使用 webbrowser,但由于某种原因我一直得到 Invalid indentation (too many levels),即使我在我的 .py 中使用了相同的 webbrowser使用 buildozer,它成功了。 我不知道出了什么问题,是否有一种特殊的方法可以在 .kv 文件中使用它,我将不胜感激。

这是我的 .py 文件:

from kivymd.app import MDApp
from kivy.core.window import Window


Window.size = (300, 530)


class MainApp(MDApp):
    def build(self):
        self.theme_cls.primary_palette = 'Gray'
        self.theme_cls.primary_hue = '200'
        self.theme_cls.theme_style = 'Dark'


if __name__ == "__main__":
    app = MainApp()
    app.run()

这是我的 .kv 文件:

MDBoxLayout:
    orientation: 'vertical'
    id: box
    MDToolbar:
        title: 'MyApp'
        pos_hint: {'top': 1}

    MDCard:
        orientation: 'vertical'
        radius: dp(8)
        size_hint: dp(1), None
        height: record_your_conversations_label1a.height
        #elevation: dp(8)
        MDLabel:
            id: record_your_conversations_label5b4
            size_hint_y: None
            height: self.texture_size[1] + 2*self.padding[1]
            padding: dp(15), dp(3)
            markup: True
            text:
                """
                The Label has halign and valign properties to control the alignment of its text. However, by default the text image (texture) is only just large enough to contain the characters and is positioned in the center of the Label. The valign property will have no effect and halign will only have an effect if your text has newlines; a single line of text will appear to be centered even though halign is set to left (by default).
                """
        MDSeparator:
            height: dp(2)

        MDLabel:
            id: record_your_conversations_label6a
            size_hint_y: None
            height: self.texture_size[1] + 2*self.padding[1]
            padding: dp(15), dp(10)
            markup: True
            text:
                """
                For many, this is the first introduction to master audio files making for a steep learning curve.\n
                I've searched for the tips [ref=Alitu][b][u]google[/u][/b][/ref], you are welcome!
                """
                on_ref_press:
                    import webbrowser
                    webbrowser.open('https://google.com')

这是回溯信息:

Traceback (most recent call last):
   File "C:\Users\user\Desktop\Projects\Lessons\templates\templates1\easy.py", line 17, in <module>
     app.run()
   File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\app.py", line 949, in run
     self._run_prepare()
   File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\app.py", line 918, in _run_prepare
     self.load_kv(filename=self.kv_file)
   File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\app.py", line 691, in load_kv
     root = Builder.load_file(rfilename)
   File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\lang\builder.py", line 306, in load_file
     return self.load_string(data, **kwargs)
   File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\lang\builder.py", line 373, in load_string
     parser = Parser(content=string, filename=fn)
   File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\lang\parser.py", line 402, in __init__
     self.parse(content)
   File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\lang\parser.py", line 511, in parse
     objects, remaining_lines = self.parse_level(0, lines)
   File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\lang\parser.py", line 614, in parse_level
     _objects, _lines = self.parse_level(
   File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\lang\parser.py", line 614, in parse_level
     _objects, _lines = self.parse_level(
   File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\lang\parser.py", line 684, in parse_level
     raise ParserException(self, ln,
 kivy.lang.parser.ParserException: Parser: File "C:\Users\user\Desktop\Projects\Lessons\templates\templates1\main.kv", line 39:
 ...
      37:                """
      38:                on_ref_press:
 >>   39:                    import webbrowser
      40:                    webbrowser.open('https://google.com')
      41:
 ...
 Invalid indentation (too many levels)
[Finished in 4.3s]

在此先感谢您的帮助..

我以为 webbrowser 应该在 text 下缩进,但我错了。它应该在 label 下缩进,如下所示:

    MDLabel:
        id: record_your_conversations_label6a
        size_hint_y: None
        height: self.texture_size[1] + 2*self.padding[1]
        padding: dp(15), dp(10)
        markup: True
        text:
            """
            For many, this is the first introduction to master audio files making for a steep learning curve.\n
            I've searched for the tips [ref=Alitu][b][u]google[/u][/b][/ref], you are welcome!
            """
        on_ref_press:
            import webbrowser
            webbrowser.open('https://google.com')