问题
In the same experiment, I tried to positionize a Label using this code:
class TetraApp(App):
def build(self):
Window.size=(875,600)
Window.clearcolor = (1, 1, 1, 1)
b = BoxLayout(orientation ='vertical')
#Here, the pos and pos hint doesnt seem to work for small values
self.lab=Label(text="How Can I Help\n You?", font_size='35',color =[0, 0, 0, 1],font_name='VarelaRound-Regular',pos=[0.1,0.01],pos_hint={'x': 0.005, 'y': 0})
self.t = TextInput(hint_text='Say Something...', size_hint=(1,0.25), multiline=False)
self.t.bind(on_text_validate=self.enterClicked)
b.add_widget(TitleBar())
b.add_widget(self.lab)
b.add_widget(self.t)
Window.borderless=True
return b
This is what i get:
回答1:
The problem is not positioning (the BoxLyout
handles that), but the size
of your Label
. You need to provide a height
for the Label
. Something like:
self.lab=Label(text="How Can I Help\n You?", font_size='35',color =[0, 0, 0, 1], font_name='VarelaRound-Regular', size_hint_y=None, height=100)
Your TitleBar
has a fixed height, the TextInput
is set to take up a quarter of the BoxLayout
height, and since the default size_hint_y
is 1.0, your Label
takes the rest of the space. Giving the Label
a fixed height, leaves the rest of the space to the TextInput
. You could use size_hint_y
instead of the fixed height, and that would allow the Label
and the TextInput
to share the space proportional to their size_hint_y
values.
来源:https://stackoverflow.com/questions/60993245/position-of-the-kivy-label