【问题标题】:How to use webbrowser in .kv file如何在 .kv 文件中使用 webbrowser
【发布时间】:2021-11-02 11:26:42
【问题描述】:

我正在尝试在我的 .kv 文件中使用 webbrowser,但由于某种原因,我不断收到 Invalid indentation (too many levels),即使我在带有 buildozer 的 .py 中使用了相同的 webbrowser,并且它有效。 我不知道出了什么问题,是否有特殊的方法可以在 .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]

提前感谢您的帮助..

【问题讨论】:

    标签: python-3.x hyperlink kivy kivymd python-webbrowser


    【解决方案1】:

    我以为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')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-29
      • 2020-11-09
      • 2021-03-16
      • 2019-12-29
      • 2021-10-05
      相关资源
      最近更新 更多