Hendrik
Hendrik

Reputation: 131

Good-styled console input

I'm writing a little Chatserver/-client to learn Python. Now I want to make the consoleinput a little bit nicer, but I don't know how to do it...

Everytime I recieve a message from the socket, I do print() in the listening thread. But then the text already entered to input() is over the printed message and the cursor is at the bottom.

What can I do, that is works like in Minecraft-Server, so the text already entered moves to the bottom? Would be great if someone can help :)

Upvotes: 3

Views: 374

Answers (1)

Eyrofire
Eyrofire

Reputation: 310

You can't get that level of control with the console, but you can use python's default tkinter to make a simple UI. Below is an example (Python 3) that I whipped up in a few minutes. You can type in messages, press send, and they will appear in the box above.

from tkinter import *
from tkinter import ttk

def send(view, entry):
    view.insert('1.0', entry.get() + "\n")

root = Tk()

msgview = Text(root, width=100, height=20)
msgview.grid(sticky=(N,E,S,W))

mymessage = StringVar(value="type here...")

msginput = Entry(root, textvariable=mymessage)
msginput.grid(sticky=(E,W))

sendbutton = ttk.Button(root, text="send",\
                        command=lambda: send(msgview, msginput))
sendbutton.grid()

root.mainloop()

I suggest looking at the tkdocs tutorial over the effbot one, since it is clearer, easier to follow and is more thorough in my opinion. New Mexico Tech also provides a great reference for tkinter here

Upvotes: 1

Related Questions