我将具有相同文本设置的文本输入和标签放置在同一位置,但文本不同。
我试图通过添加标签并将文本输入放在其上并使其透明来选择标签。我还限制标签宽度。我尝试将文本输入大小调整为标签文本的纹理大小,但这没有帮助。我预计来自文本输入和标签的文本将位于同一位置,因此文本输入选择在标签文本周围将是准确的。 结果:
代码:
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.floatlayout import FloatLayout
class main(App):
def build(self):
f = FloatLayout()
# def changesmth(i):
# i._label.refresh()
# x,y = i._label.texture.size
# textinput2.size = (x,y)
# xp, yp = i.pos
# textinput2.pos=(xp-50,yp-10)
# textinput2.line_spacing = 5
label = Label(text="The quick brown fox jumps over the lazy dog",text_size = (400, None) ,markup=True,font_size="24", size=(400, 100),pos=(100,100), size_hint=(None, None))
textinput2 = TextInput(text="The quick brown fox jumps over the lazy dog", font_size="24",size=(400,100),pos=(100,100), size_hint=(None, None), readonly=True, foreground_color= (1,1,1,0.5), background_color = (1,0,0,0.1))
#changesmth(label)
f.add_widget(textinput2)
f.add_widget(label)
return f
main().run()
请不要使用.kv语言来回答。谢谢。
只需设置一些属性。尝试改变:
label = Label(text="The quick brown fox jumps over the lazy dog",text_size = (400, None) ,markup=True,font_size="24", size=(400, 100),pos=(100,100), size_hint=(None, None))
textinput2 = TextInput(text="The quick brown fox jumps over the lazy dog", font_size="24",size=(400,100),pos=(100,100), size_hint=(None, None), readonly=True, foreground_color= (1,1,1,0.5), background_color = (1,0,0,0.1))
至:
label = Label(text="The quick brown fox jumps over the lazy dog",text_size = (400, 100), markup=True,
font_size="24", size=(400, 100),pos=(100,100), size_hint=(None, None), valign='top')
textinput2 = TextInput(text="The quick brown fox jumps over the lazy dog", font_size="24",size=(400,100),
pos=(100,100), size_hint=(None, None), readonly=True, foreground_color= (1,1,1,0.5),
background_color = (1,0,0,0.1), padding=0)
主要更改是
text_size
的 valign
和 Label
设置以及 padding
的 TextInput
设置。