Search code examples
python-3.xhyperlinkkivykivymdpython-webbrowser

How to use webbrowser in .kv file


I am trying to use webbrowser in my .kv file, but for some reason I keep getting Invalid indentation (too many levels), even though I have used the same webbrowser in my .py with buildozer, and it worked. I don't know what is wrong, is there a special way to use it in the .kv file I'd appreciate any help.

here's my .py file:

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()

here's my .kv file:

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')

And here's the traceback meassge:

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]

Thanks in advance for help..


Solution

  • I thought the webbrowser was suppose to be indented under text, but I was wrong. It should be indented under label, like this:

        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')