带有输入文本的标签在kivy中的位置

时间:2017-03-13 19:19:24

标签: python input label kivy

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

select partid, sum(price) as totalprice
from t
where [status] = 1
group by partid

实施例。来自txt文件的数据:

    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)

1 个答案:

答案 0 :(得分:0)

发生了这种情况,因为您的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()

结果我们得到了:

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