我想在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