【问题标题】:Position of label with input text in kivy带有输入文本的标签在kivy中的位置
【发布时间】:2017-03-13 19:19:23
【问题描述】:

锚标签的位置有问题。 Kivy 在中间/中心运行此代码。它是屏幕显示的一部分。

    with open('weatherdata.txt', encoding='utf-8') as weatherdata:
        read_weatherdata = weatherdata.read()

    label_position = AnchorLayout(anchor_x='right',
                                  anchor_y='bottom')
    label_settings = Label(text=read_weatherdata,
                           font_size='12sp',
                           size=(200, 200),
                           color=(0.4, 0.4, 0.4, 1))
    label_position.add_widget(label_settings)
    self.add_widget(label_position)

例如。 txt文件中的数据:

Weather now in Warsaw, pl 

Clouds: 20 %
Rain: 15 %
Wind speed: 2.6
Wind degree: 340
Humidity: 75 %
Temperature: 5.0 celsius
Max temperature: 5.0 celsius
Min temperature: 5.0 celsius
Weather status: few clouds

【问题讨论】:

    标签: python input label kivy


    【解决方案1】:

    这是因为您的Label 与屏幕大小相同。为了防止这种情况,你应该这样做size_hint: None, None

    我建议您使用kv lauguage,因为它有助于使您的代码看起来更干净

    这是一个改进的代码示例:

    from kivy.app import App
    from kivy.lang import Builder
    from kivy.uix.anchorlayout import AnchorLayout
    
    Builder.load_string('''
    <Root>:
        anchor_x: 'right'
        anchor_y: 'bottom'
    
        Label:
            id: weather_info
            color: 0.4, 0.4, 0.4, 1
            font_size: '12sp'
            size_hint: None, None
            size: self.texture_size # you can specify you own size here now
    ''')
    
    class Root(AnchorLayout):
        pass
    
    class TestApp(App):
        def build(self):
            weather_data=\
    '''
    Weather now in Warsaw, pl
    
    Clouds: 20 %
    Rain: 15 %
    Wind speed: 2.6
    Wind degree: 340
    Humidity: 75 %
    Temperature: 5.0 celsius
    Max temperature: 5.0 celsius
    Min temperature: 5.0 celsius
    Weather status: few clouds
    '''
    
            self.root = Root()
            self.root.ids.weather_info.text = weather_data
            return self.root
    
    
    TestApp().run()
    

    结果我们得到:

    (字体太小太暗,但您可以随时轻松更改!)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-09
      • 2020-12-05
      • 2014-09-12
      • 1970-01-01
      • 2015-07-30
      相关资源
      最近更新 更多