我正在使用Python进行聊天程序,并希望为我的客户端添加一些用户友好的界面。事实是我给了自己挑战 仅使用终端。
所以我找到了urwid
模块,这是一个跨平台的,在网上有详细记录。
阅读完手册并观看模块教程后,我真的不知道如何编写这个界面,但我获得了一些关于理论的知识(Widgets,不同类型的对象,屏幕是如何分区的......)
所以我最终在stackoverflow或github上找到了一些代码片段,我找到了一个listBox
示例,它将真正帮助我保存屏幕中的日志部分。
现在我需要在底部创建一个永久输入区域以从用户那里获取输入。我没有找到任何代码或讨论如何做到这一点。 如何在底部创建一个永久输入区域以接受用户的输入?
任何链接或代码示例将不胜感激! :)
谢谢大家, 埃利奥特
答案 0 :(得分:3)
tutorial有几个自包含的示例,展示了一些基本功能。
对于一种简单的方法,我建议使用Frame
对象,focus_part
设置为'footer'
。将提示文本移动到主窗口的基本示例:
import urwid
text_str = 'Here are a few previous lines.of text that populate.the main terminal window.Press "return" to add the prompt line to the main window.or press escape to exit.'.replace('.', '\n')
def main():
my_term = MyTerminal()
urwid.MainLoop(my_term).run()
class MyTerminal(urwid.WidgetWrap):
def __init__(self):
self.screen_text = urwid.Text(text_str)
self.prompt_text = urwid.Edit('prompt: ', '')
self._w = urwid.Frame(header=urwid.Pile([urwid.Text('header text'),
urwid.Divider()]),
body=urwid.ListBox([self.screen_text]),
footer=self.prompt_text,
focus_part='footer')
def keypress(self, size, key):
if key is 'esc':
raise urwid.ExitMainLoop()
if key == 'enter':
self.screen_text.set_text(self.screen_text.text +
'\n' +
self.prompt_text.edit_text)
self.prompt_text.edit_text = ''
return
super(MyTerminal, self).keypress(size, key)
main()