【问题标题】:How to center text horizontally in a Kivy text input?如何在 Kivy 文本输入中水平居中文本?
【发布时间】:2016-07-17 13:28:02
【问题描述】:

我想在 Kivy 文本输入中将单行文本居中。 我将使用填充

widget.padding = [ (self.textinput.width - width of line) / 2, 20, 0, 0]

但我找不到线的宽度。如何计算或访问线的宽度?

【问题讨论】:

  • 我想知道你能不能把它放在一个 3x3 网格布局的中心正方形?

标签: python kivy


【解决方案1】:

有一个内部TextInput._get_text_width 方法可用于计算正确的填充:

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.lang import Builder

Builder.load_string('''

<MyWidget>:
    TextInput:
        multiline: False
        on_text: root.update_padding(args[0])
        padding_x: self.width/2 # initial padding
''')

class MyWidget(FloatLayout):
    def update_padding(self, text_input, *args):
        text_width = text_input._get_text_width(
            text_input.text,
            text_input.tab_width,
            text_input._label_cached
        )
        text_input.padding_x = (text_input.width - text_width)/2

class MyApp(App):
    def build(self):        
        return MyWidget()

if __name__ == '__main__':
    MyApp().run()

【讨论】:

    【解决方案2】:

    上面的解决方案几乎对我有用。有时填充不会正确更新。这是我的细微改动,将 text_width 设置为 NumericProperty:

    在基维:

    <CenteredTextInput@TextInput>:
        multiline: False
        on_text: root.update_padding()
        padding_x: (self.width - self.text_width) / 2
    

    在 Python 中:

    class CenteredTextInput(TextInput):
        '''
        A centered TextInput.
        '''
    
        text_width = NumericProperty()
        '''The text width
        '''
    
        def update_padding(self, *args):
            '''
            Update the padding so the text is centered
            '''
            self.text_width = self._get_text_width(
                self.text,
                self.tab_width,
                self._label_cached
            )
    

    【讨论】:

      【解决方案3】:

      您可以在按钮后面制作文本输入,并将按钮可视化为文本输入。

      按下按钮时,将焦点放在文本输入上,并更新按钮文本。

      我在这里做了一个例子。

      from kivy.uix.textinput import TextInput
      from kivy.uix.boxlayout import BoxLayout
      from kivy.uix.floatlayout import FloatLayout
      from kivy.uix.button import Button
      from kivy.uix.label import Label
      from kivy.clock import Clock
      from kivy.app import App
      from kivy import require
      
      require('1.9.1')
      
      
      class MyWidget(BoxLayout):
          def __init__(self,**kwargs):
              super(MyWidget,self).__init__(**kwargs)
      
              self.orientation = "vertical"
      
              self.cur = False
      
              self.textinput = TextInput(text='',halign="center",multiline=False)
              self.textinput.bind(text=self.on_text)
      
              self.button = Button(background_normal="",background_color=[0,0,0.1,1],font_size="40sp")
              self.button.bind(on_release=self.button_click)
      
              self.my_float_layout = FloatLayout()
      
              self.my_float_layout.add_widget(self.textinput)
              self.my_float_layout.add_widget(self.button)
      
              self.add_widget(Label(text="type text below",font_size="40sp"))
              self.add_widget(self.my_float_layout)
      
              Clock.schedule_interval(self.cursor, 0.5)
      
      
          def cursor(self,dt):          # function to visualize a cursor
              if self.textinput.focus:
                  cur_pos = self.textinput.cursor[0]
                  if not self.cur:
                      self.button.text = self.textinput.text[:cur_pos] + "|" + self.textinput.text[cur_pos:]
                      self.cur = True
                  else:
                      self.button.text = self.textinput.text[:cur_pos] + " " + self.textinput.text[cur_pos:]
                      self.cur = False
              elif self.cur:
                  self.button.text = self.textinput.text + " "
                  self.cur = False
      
      
      
          def on_text(self, *args):     # function to set the button text
              self.button.text = self.textinput.text
      
      
          def button_click(self,*args): # function to focus the input
              self.textinput.focus = True
      
      
      
      class MyApp(App):
          def build(self):
              return MyWidget()
      
      
      if __name__ == "__main__":
          MyApp().run()
      

      【讨论】:

      • 非常感谢您的回答。你真的很有创意。
      • @mnrl 这里有问题。如果将光标向左移动,则可视化光标不会这样做。我会尝试解决这个问题,并更新答案。
      猜你喜欢
      • 2011-03-29
      • 1970-01-01
      • 2016-02-21
      • 1970-01-01
      • 2019-08-05
      • 2019-07-26
      • 2023-04-10
      • 2018-04-26
      • 2016-08-06
      相关资源
      最近更新 更多