你真的应该听从@eyllanesc 的建议。但这是一种做你想做的事情的方法(如果我正确地解释了你的问题):
from functools import partial
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.app import runTouchApp
from kivy.uix.textinput import TextInput
class RootWidget(GridLayout):
def __init__(self, **kwargs):
# prevent override
super(RootWidget, self).__init__(**kwargs)
self.cols = 1
self.email_label = Label(
color=(1, .5, .5, 1),
text="Email:",
size_hint=(1, 1)
)
self.add_widget(self.email_label)
self.email = TextInput(
text='',
foreground_color=(1, .5, .5, 1),
multiline=False,
size_hint=(1, 1))
self.add_widget(self.email)
self.add_widget(
Label(
color=(1, .5, .5, 1),
text="Password:",
size_hint=(1, 1)))
self.pw = TextInput(
text='',
foreground_color=(1, .5, .5, 1),
multiline=False,
password=True,
size_hint=(1, 1))
self.add_widget(self.pw)
self.login = Button(
color=(1, .5, .5, 1),
background_color=(0, 0, 0, 1),
text="Login",
size_hint=(1, 4))
self.add_widget(self.login)
self.login.bind(
on_press=partial(
self.checkuser,
self.email,
self.pw))
self.bind(size=self.do_resize)
def checkuser(self, *args):
pass
def do_resize(self, rootWidgt, new_size):
self.email_label.font_size = 0.025 * new_size[1]
if __name__ == '__main__':
runTouchApp(RootWidget())
简单地说,保存对要动态调整的内容的引用,添加绑定以在调整 RootWidget 大小时调用 do_resize(),然后将代码放入其中以进行所需的调整。注意do_resize方法会在RootWidget第一次显示时被调用。