如何使用Python Textual框架获取多行输入

huangapple go评论121阅读模式
英文:

How to get a multiline input with python Textual framework

问题

I'm using the Textual-framework in Python and I want the user to enter a multiline Text.
However, I cannot find a widget that does the trick.

With the Input widget one can only enter one line. I tried to tweak it, but I cannot fix it so it wraps the text when it overflows.
Here is a minimal example. I also tried the overflow style, but it doesn't work.

  1. from textual.app import App
  2. from textual.binding import Binding
  3. from textual.widgets import Input
  4. class MultiLineInput(Input):
  5. BINDINGS = [Binding("enter", "newline", "New line", show=True)]
  6. def action_newline(self):
  7. self.value += "\n"
  8. self.cursor_position = len(self.value)
  9. def on_mount(self):
  10. self.styles.height = "4"
  11. class Minimal(App):
  12. def compose(self):
  13. yield MultiLineInput(id="input")
  14. if __name__ == "__main__":
  15. app = Minimal()
  16. app.run()
英文:

I'm using the Textual-framework in Python and I want the user to enter a multiline Text.
However, I cannot find a widget that does the trick.

With the Input widget on can only enter one line. I tried to tweak it, but I cannot fix it so it wraps the text, when it overflows.
Here is a minimal example. I also tried the overflow style, but it doesn't work.

  1. from textual.app import App
  2. from textual.binding import Binding
  3. from textual.widgets import Input
  4. class MultiLineInput(Input):
  5. BINDINGS = [Binding("enter", "newline", "New line", show=True)]
  6. def action_newline(self):
  7. self.value += "\n"
  8. self.cursor_position = len(self.value)
  9. def on_mount(self):
  10. self.styles.height="4"
  11. class Minimal(App):
  12. def compose(self):
  13. yield MultiLineInput(id="input")
  14. if __name__ == "__main__":
  15. app = Minimal()
  16. app.run()

答案1

得分: 1

查看新的文本 TextArea 小部件。

英文:

See the new Textual TextArea widget.

huangapple
  • 本文由 发表于 2023年5月28日 05:02:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76349029.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定