Kivy中TextInput与Label同设置却出现异常缩进问题求助
问题:TextInput与Label文本位置错位,TextInput出现异常缩进
我尝试让TextInput和Label的文本位置完全对齐,通过Label的_label.texture.size计算限定宽度下的文本宽高,再把这个尺寸应用到两者上,目的是实现Label文本的精准选择,但TextInput出现了奇怪的缩进,导致文本错位。
问题表现:TextInput中的文本整体向右偏移,无法与Label的文本对齐。
原代码
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() text = "This is random text Every time you're online, you are bombarded by pictures, articles, links and videos trying to tell their story. Unfortunately, not all of these stories are true. Sometimes they want you to click on another story or advertisement at their own site, other times they want to upset people for political reasons. " labeltocheckheight = Label(text=text, text_size=(200, None), markup=True,font_size="24", size=(400, 100), pos=(100, 100), size_hint=(1, 1), valign='top') labeltocheckheight._label.refresh() width, height = labeltocheckheight._label.texture.size label = Label(text=text, text_size=(width, height), markup=True,font_size="24", size=(width, height), pos=(100, 100), size_hint=(None, None), valign='top') textinput2 = TextInput(text=text, font_size="24", size=(width, height), cursor=(0,0),pos=(100, 100), size_hint=(None, None), readonly=True, foreground_color=(1, 1, 1, 0.5),background_color=(1, 0, 0, 0), padding=0) f.add_widget(textinput2) f.add_widget(label) return f main().run()
解决方案
问题根源
- TextInput默认存在内部内边距,仅设置
padding=0可能无法完全清除偏移; - Label和TextInput未统一设置水平对齐参数,导致排版逻辑不一致;
- 依赖私有属性
_label获取尺寸存在兼容性风险,且Label的text_size设置固定高度会干扰渲染; - TextInput的换行模式未与Label匹配,进一步放大排版差异。
修改后的代码
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() text = "This is random text Every time you're online, you are bombarded by pictures, articles, links and videos trying to tell their story. Unfortunately, not all of these stories are true. Sometimes they want you to click on another story or advertisement at their own site, other times they want to upset people for political reasons. " # 使用公开API获取文本尺寸,避免依赖内部实现 labeltocheckheight = Label( text=text, text_size=(200, None), markup=True, font_size="24", valign='top', halign='left' ) labeltocheckheight.texture_update() width, height = labeltocheckheight.texture_size # Label使用计算宽度,高度自动适配文本 label = Label( text=text, text_size=(width, None), markup=True, font_size="24", size=(width, height), pos=(100, 100), size_hint=(None, None), valign='top', halign='left' ) textinput2 = TextInput( text=text, font_size="24", size=(width, height), cursor=(0,0), pos=(100, 100), size_hint=(None, None), readonly=True, foreground_color=(1, 1, 1, 0.5), background_color=(1, 0, 0, 0), padding=0, padding_x=0, padding_y=0, valign='top', halign='left', wrap_mode='word' ) f.add_widget(textinput2) f.add_widget(label) return f main().run()
关键修改点
- 替换私有属性调用,用公开的
texture_update()和texture_size获取文本尺寸; - 给Label和TextInput统一设置
halign='left',确保水平对齐逻辑一致; - 给TextInput额外设置
padding_x=0和padding_y=0,彻底清除内边距; - Label的
text_size设为(width, None),让高度自动适配文本,避免固定高度导致的偏差; - TextInput设置
wrap_mode='word',匹配Label的换行逻辑。
内容的提问来源于stack exchange,提问作者Mango
相关产品推荐
相关产品推荐

