[英]Position of label with input text in kivy
我對錨標簽的位置有疑問。 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
發生這種情況是因為您的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()
結果,我們得到:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.