我想在Kivy中開發一個電子郵件應用程序,基本上只是一個練習來學習框架的輸入和輸出......我試圖創建初始窗口並已達到一個絆腳石!這個想法是,它只會在收件箱中顯示電子郵件列表,就像移動設備上的任何基本電子郵件應用程序一樣。Kivy按鈕文本對齊問題
我遇到的問題是我無法弄清楚如何讓每個列表項(正好是一個按鈕)的文本正確對齊。在我的按鈕中使用「halign ='left'」將使文本左對齊,但僅相對於每個按鈕;它仍然位於每個按鈕的中心。
我的實際應用程序有點大,所以這是一個快速和骯髒的例子,我從股票Kivy的例子。 (我意識到這個代碼並不完美......就像我說的快速和骯髒的例子一樣...它雖然工作!)所以,你可以看到,每個按鈕上的兩行文字相互對齊,但它們並不全部對齊。任何人都可以建議我會做什麼,使每個按鈕左側的所有文本對齊,比如說10px?我確實在StackOverflow上找到了一個相關的聲音項目,但它並沒有真正回答這個問題,例如,它似乎更多地處理按鈕上的圖像。我是Kivy的新手,但我已經閱讀了教程和文檔,並且廣泛搜索了Google - 所以任何幫助都將不勝感激!
import kivy
kivy.require('1.0.8')
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.button import Button
from kivy.uix.scrollview import ScrollView
from kivy.uix.gridlayout import GridLayout
import random
class ScrollViewApp(App):
def build(self):
# create a default grid layout with custom width/height
layout = GridLayout(cols=1, spacing=10, size_hint=(None, None),
width=Window.width)
# when we add children to the grid layout, its size doesn't change at
# all. we need to ensure that the height will be the minimum required to
# contain all the childs. (otherwise, we'll child outside the bounding
# box of the childs)
layout.bind(minimum_height=layout.setter('height'))
# add button into that grid
for i in range(30):
btn = Button(text=str(i * random.random()) + '\n' + str(i * random.random()),
size=(300, 40),
size_hint=(None, None),
halign='left')
layout.add_widget(btn)
# create a scroll view, with a size < size of the grid
root = ScrollView(size_hint=(None, None))
root.size = (Window.width, Window.height)
root.center = Window.center
root.add_widget(layout)
return root
if __name__ == '__main__':
ScrollViewApp().run()
另外,有一個看text_align例子會有所幫助。 – Tshirtman 2013-03-11 13:27:09