Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Help with dialog field
-
import clipboard import dialogs text = dialogs.input_alert("Text") half = len(text) // 2 first = text[:half] second = text[half:] end = "&a" + first + "&f" + second clipboard.set(end)
Hello, this script is making a text half green and half white (the &a is green &f is white, it only works on a specific Minecraft server) it all works in the console but in the Pythonista keyboard it doesn’t open the dialog field, where you input your text
-
@ProfessorFlaw
input_alert
and similar functions are not available in the custom keyboard because they would require showing the system keyboard, which isn't possible in custom keyboards. I realize that this is unfortunate, but right now, this simply isn't supported. -
@omz do you have a idea of what I could use instead?
-
@ProfessorFlaw Here's a possible solution. The
kb_input_alert
function below allows you to get a short text input, using the on-screen (PyKeys) keyboard. It doesn't support cursor placement or text selection, so it's only really suitable for short input, but I hope it's still helpful.import keyboard import ui import dialogs class KeyboardTypingView(ui.View): def __init__(self, *args, **kwargs): self.label = ui.Label(frame=self.bounds, flex='wh') self.label.font = ('<System>', 20) self.placeholder = ui.Label(frame=self.bounds.inset(0, 8, 0, 8), flex='wh') self.placeholder.font = ('<System>', 20) self.add_subview(self.label) self.add_subview(self.placeholder) self.label.text = '' self.placeholder.alpha = 0.3 self.cursor = ui.View(bg_color='#63aaff') self.add_subview(self.cursor) self.update_cursor() self.entered_text = '' super().__init__(*args, **kwargs) def kb_should_insert(self, text): if text == '\n': # Return pressed self.entered_text = self.label.text keyboard.set_view(None) self.label.text += text self.update_cursor() return '' def update_cursor(self): self.placeholder.hidden = len(self.label.text) > 0 self.label.size_to_fit() self.cursor.frame = (self.label.frame.max_x, 0, 3, self.bounds.h) def kb_should_delete(self): self.label.text = self.label.text[0:-1] self.update_cursor() return False def kb_input_alert(title='Enter text'): if not keyboard.is_keyboard(): return dialogs.input_alert(title) v = KeyboardTypingView() v.placeholder.text = title keyboard.set_view(v) v.wait_modal() return v.entered_text def main(): name = kb_input_alert('Enter your name') dialogs.hud_alert(f'Hello, {name}!') if __name__ == '__main__': main()```
-
@omz nice, a little change to be sure that keyboard is visible
keyboard.set_view(v, mode='minimized')
-
@omz
Thank you very much