kivy入门之布局(四)
本篇不介入kv语言,直接在主程序堆砌控件
1 from kivy.app import App 2 from kivy.uix.label import Label 3 from kivy.core.text import LabelBase 4 from kivy.uix.textinput import TextInput 5 from kivy.uix.gridlayout import GridLayout 6 7 LabelBase.register("Roboto", "msyhl.ttc") 8 9 10 class LoginScreen(GridLayout): 11 """主布局,加入相关控件对象""" 12 13 def __init__(self): 14 super().__init__() 15 self.cols = 2 16 self.add_widget(Label(text="用户名")) 17 self.username = TextInput(multiline=False) 18 self.add_widget(self.username) 19 self.add_widget(Label(text="密码")) 20 self.password = TextInput(password=True, multiline=False) 21 self.add_widget(self.password) 22 23 24 class Test(App): 25 def build(self): 26 """重写该方法""" 27 # 返回根控件位置 28 return LoginScreen() 29 30 31 if __name__ == '__main__': 32 Test().run()
运行界面显示: