python怎么居中输入文字_如何在Kivy文本输入中水平居中文本?

您可以在按钮后面创建一个textinput,并将该按钮可视化为文本输入。在

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

我在这里举了个例子。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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值