Как использовать веб-браузер в файле .kv

#python-3.x #hyperlink #kivy #kivymd #python-webbrowser

Вопрос:

Я пытаюсь использовать webbrowser в своем файле .kv, но по какой-то причине я продолжаю получать Invalid indentation (too many levels) , хотя я использовал то же webbrowser самое в своем файле .py с 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:UsersuserDesktopProjectsLessonstemplatestemplates1easy.py", line 17, in lt;modulegt;  app.run()  File "C:UsersuserAppDataLocalProgramsPythonPython39libsite-packageskivyapp.py", line 949, in run  self._run_prepare()  File "C:UsersuserAppDataLocalProgramsPythonPython39libsite-packageskivyapp.py", line 918, in _run_prepare  self.load_kv(filename=self.kv_file)  File "C:UsersuserAppDataLocalProgramsPythonPython39libsite-packageskivyapp.py", line 691, in load_kv  root = Builder.load_file(rfilename)  File "C:UsersuserAppDataLocalProgramsPythonPython39libsite-packageskivylangbuilder.py", line 306, in load_file  return self.load_string(data, **kwargs)  File "C:UsersuserAppDataLocalProgramsPythonPython39libsite-packageskivylangbuilder.py", line 373, in load_string  parser = Parser(content=string, filename=fn)  File "C:UsersuserAppDataLocalProgramsPythonPython39libsite-packageskivylangparser.py", line 402, in __init__  self.parse(content)  File "C:UsersuserAppDataLocalProgramsPythonPython39libsite-packageskivylangparser.py", line 511, in parse  objects, remaining_lines = self.parse_level(0, lines)  File "C:UsersuserAppDataLocalProgramsPythonPython39libsite-packageskivylangparser.py", line 614, in parse_level  _objects, _lines = self.parse_level(  File "C:UsersuserAppDataLocalProgramsPythonPython39libsite-packageskivylangparser.py", line 614, in parse_level  _objects, _lines = self.parse_level(  File "C:UsersuserAppDataLocalProgramsPythonPython39libsite-packageskivylangparser.py", line 684, in parse_level  raise ParserException(self, ln,  kivy.lang.parser.ParserException: Parser: File "C:UsersuserDesktopProjectsLessonstemplatestemplates1main.kv", line 39:  ...  37: """  38: on_ref_press:  gt;gt; 39: import webbrowser  40: webbrowser.open('https://google.com')  41:  ...  Invalid indentation (too many levels) [Finished in 4.3s]  

Заранее спасибо за помощь..

Ответ №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')