【问题标题】:Label kivy does not display text in List form标签 kivy 不以列表形式显示文本
【发布时间】:2020-11-27 16:25:22
【问题描述】:

我想在 kivy 标签中打印一个列表,但是当我运行代码时,它只显示列表的第一行。如何让 kivy 打印整个列表?

from kivy.uix.label import Label
from kivy.uix.screenmanager import ScreenManager,Screen
Import requests
from kivy.lang import Builder
from kivy.uix.scrollview import ScreollView
from kivymd.uix.button import RoundFlatButton 
from kivymd.uix.snackbar import Snackbar
from kivy.app import App

Build.load_string('''
<Page>:
    ScrollView:
        Label:
            id: label
            text: ''
            height: self.texture_size[1]
            text_size: self.width, None
            theme_text_color: "Custom"
            text_color: 1, 1, 1, 1
            font_size: 20
            size_hint: 1, 4
         MDRoundFlatButton:
                text: 'push'
                pos_hint: {'center_x':  .5, 'center_y':  .5}
                on_press: robot.problem()
'''

Class Page(Screen):
    def __init__(self, **kwargs):
         súper(Page, self).__init__(**kwargs)
    def problem(self):
        try:
            objetivo = requests.get(url=self.ids.t1.text)#textinput       
            header = dict(objetivo.headers)
            for x in header:
                self.ids.label.text = x + ' : ' + header[x]
                return self.ids.label.text
        except: 
            self.ids.label.text = '''  ERROR
            (https://www.example.com)'''
            self.ids.labe.text = 'swipe'
            Snackbar(text='         URL ERROR!').show()
            return self.ids.label.text

Class App(App):
    def build(self, **kwargs):
     self.theme_cls.theme_style = 'Dark'
      H = ScreenManager()
      G = Page(mame='hello')
      H.add_widget(G)
      return H

App().run()

它只显示了一行代码,它应该显示完整的列表,请帮助!

【问题讨论】:

    标签: python kivy kivymd


    【解决方案1】:

    您需要做的一件事是设置标签的 ma​​x_lines 属性。

    例如,这是我刚刚作为 .kv 文件的一部分进行测试的内容

    Label:
        size_hint_y: 5
        text: 'Experiment Notes\nnewline\n3rd line'
        max_lines: 10
    

    我不确切知道您的代码在做什么,但您可能需要在文本字符串中添加换行符。

    我建议的另一件事是在构建字符串时使用临时变量,然后在完成任何循环后,只分配一次 text 属性。

    things = ('one line', 'second line', '3rd line', )
    my_string = '\n'.join(things)
    my_label_object.text = my_string
    

    【讨论】:

    • 感谢您的回答,我已经尝试过您的解决方案,但它仍然对我不起作用,当我尝试打印 for 循环的内容时,您会看到它没有将内容显示为列表,示例:(对于 x 示例:self.ids.exp.text = x)那时它应该以列表形式显示 x,但它只显示第一行代码:(
    【解决方案2】:

    我解决了:

    self.ids.label.text = dictionary['example']
    

    我刚刚转换为字典并打印每个键给我它的值等等

    【讨论】:

      猜你喜欢
      • 2018-02-06
      • 1970-01-01
      • 1970-01-01
      • 2022-12-06
      • 2017-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多