2

I'm working on a chat program in Python and would like to add some user friendly interface to my client. The fact is that I gave myself the challenge of using only terminal.

So I found the urwid module to work with, which is cross-platform and well-documented online.

After reading the manual and watching the tutorial of the module I didn't really know how to write this interface but I acquired some knowledge about the theory (Widgets, different types of object, how is the screen partitioned...)

So I ended up finding some pieces of code on stackoverflow or github, I found a listBox example which will really help me for the logs-keeping part of the screen.

Now I need to create a permanent input area on the bottom to take input from the user. I did not find any code or discussion on how to do this. How can I create a permanent input area at the bottom to accept input from the user?

Any links or examples of code will be appreciated! :)

Thanks everybody, Elliot

Steven C. Howell
  • 16,902
  • 15
  • 72
  • 97
  • Welcome to StackOverflow! In an effort to help you improve your question, try to be more concise in what you write to avoid a "wall of text" not necessary for the question. Focus on "What are you trying to do?", "What have you tried?", "What error are you getting?", "What references could you link to that might be relevant?". – Steven C. Howell Apr 05 '18 at 19:37
  • rhank you man for your help :) – Elliot Alderson Apr 05 '18 at 19:39
  • zulip-terminal is a Zulip client which uses urwid, you may have a look at their code: https://github.com/zulip/zulip-terminal/blob/master/zulipterminal/ui.py – Elias Dorneles Apr 06 '18 at 10:25

1 Answers1

3

The tutorial has several self contained examples that demonstrate some basic features.

For a simple approach, I might suggest using a Frame object, with focus_part set to 'footer'. A basic example that moves the prompt text to the main window:

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()
Elias Dorneles
  • 22,556
  • 11
  • 85
  • 107
anon01
  • 10,618
  • 8
  • 35
  • 58