简体   繁体   中英

Kivy TextInput cursor control problems

I'm trying to make a quick fix for the black-line TextInput glitch ( issue ). I want it to insert a newline and move the cursor to the next line whenever the 100th character in the line is typed. It does insert the newline, but doesn't move the cursor for some reason. How do I do it?

Here is the code:

from kivy.app import App
from kivy.uix.textinput import TextInput

class MessageInput(TextInput):
    def __init__(self, **kwargs):
        super(MessageInput, self).__init__(**kwargs)

    def on_text(self, inst, text):
        if len(inst._lines[-1]) == 100:
            self.text += "\n"
            inst.cursor = (0, inst.cursor_row + 1)

class TestApp(App):
    def build(self):
        tx = MessageInput()
        return tx

TestApp().run()

I don't know how to change it without writing a custom function that handles inserting text the way it should work by default. I can tell you why it doesn't work however and it's because of this and particulary the line right here that does the same as you, but a way sooner. Therefore your code will not work if you type something.

Your code works when you paste something into TextInput , because (I think) the function insert_text() isn't called when you use clipboard.

Now more about that issue/glitch. It's good the way it is, because it limits one line/word to be as "long", as your Texture max size . It may be a problem if you want to rape TextInput's single line with passing for example a image bytes into that(I doubt someone would like to do that, though I personally tried it :D ).

And even if it's multiline=True , it'll automatically put your next word(ie when you insert a <space> or other word-breaking character) to the new line and then the whole VRAM/Texture max size stuff restarts(because of new texture probably), which is quite reasonable.

Also to explain it a little bit more: You can't limit your solution to a number of characters, because take for example android-device and PC... My laptop has Texture max size <16384> , though my phone has 4096. That's an enormous difference for solution like this. You need to get something that would tell you either texture size or vram is exceeded ie using OpenGL api most likely + akshayaurora's purposed solution is better and will be less painful to implement than checking for exceeding memory, though it might be necessary for devices with too small texture max size/vram.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM