I am learning kivy by doing atm, trying my hand at game scorekeeping app. Making some progress, but a small thing has me stumped: When giving my textbox rounded edges, afterwards I see no more key cursor. As a quick and dirty workaround, I made it so all text is selected on click, but is is still awkward to see no key cursor for when you just want to fix a typo.
Any ideas how I can get it back?
Here is my code:
from kivy.app import App
from kivy.properties import StringProperty
from kivy.uix.gridlayout import GridLayout
class PlayerGrid(GridLayout):
text_input_str = StringProperty("foo")
class GameScoreExApp(App):
pass
GameScoreExApp().run()
And the gamescoreex.kv:
#: import Clock kivy.clock.Clock
PlayerGrid:
<PlayerGrid>:
cols: 2
rows: 2
spacing: 10
TextInput:
background_color: (0,0,0,1) if self.focus else (0,0,0,1)
size_hint: 1, 1
id: player_1
text: "Player 1"
multiline: False
#on_text_validate: root.validate_player(self)
on_focus: Clock.schedule_once(lambda dt: self.select_all()) if self.focus else None
halign: 'center'
padding_y: [self.height / 2.0 - (self.line_height / 2.0) * len(self._lines), 0]
canvas.before:
Color: # button color
rgba: 0,1,1,1
RoundedRectangle:
pos: self.pos
size: self.size
radius: [30, 30, 30, 30]
Color: # text color
rgba: self.foreground_color
# self.disabled_foreground_color if self.disabled else (self.hint_text_color if not self.text else self.foreground_color)
TextInput:
id: player_2
text: "Player 2"
multiline: False
on_text_validate: root.validate_player(self)
halign: 'center'
padding_y: [self.height / 2.0 - (self.line_height / 2.0) * len(self._lines), 0]
I suggest using a custom version of TextInput
where you can redefine the style for it. To do this, add a new class (perhaps named MyTextInput
):
class MyTextInput(TextInput):
pass
Then add a rule in your kv
to define its style:
<-MyTextInput>:
background_color: (0,0,0,1)
multiline: False
halign: 'center'
padding_y: [self.height / 2.0 - (self.line_height / 2.0) * len(self._lines), 0]
canvas.before:
Color:
rgba: 0,1,1,1
RoundedRectangle:
pos: self.pos
size: self.size
radius: [30, 30, 30, 30]
Color:
rgba:
(self.cursor_color
if self.focus and not self._cursor_blink
and int(self.x + self.padding[0]) <= self._cursor_visual_pos[0] <= int(self.x + self.width - self.padding[2])
else (0, 0, 0, 0))
Rectangle:
pos: self._cursor_visual_pos
size: root.cursor_width, -self._cursor_visual_height
Color:
rgba: self.disabled_foreground_color if self.disabled else (self.hint_text_color if not self.text else self.foreground_color)
The leading -
indicates that you are defining the style of the MyTextInput
class and the style of the TextInput
should be ignored. See the documentation.
Then, in your kv
, replace the TextInput
with MyTextInput
:
PlayerGrid:
<PlayerGrid>:
cols: 2
rows: 2
spacing: 10
MyTextInput:
id: player_1
text: "Player 1"
TextInput:
id: player_2
text: "Player 2"
multiline: False
on_text_validate: root.validate_player(self)
halign: 'center'
padding_y: [self.height / 2.0 - (self.line_height / 2.0) * len(self._lines), 0]