同时使用input()和print()(例如在聊天中) [英] Use input() and print() at the same time (like in a chat)

查看:223
本文介绍了同时使用input()和print()(例如在聊天中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Python,因此我决定通过在带有服务器和客户端的终端中编写小型聊天来进行一些练习. 但是有一个问题,要进行真正的聊天,客户端必须能够像这样同时输入,发送和接收消息:

//MESSAGES
>bla bla to send

因此,消息在外壳底部滚动时,有我的输入.

我已经在网上搜索了如何实现这一目标,也许是多线程的. input()功能会阻止该过程.

如果您对我的问题有答案,那就太好了! 预先感谢!

解决方案

在Unix系统上,您可能想查看诅咒,这是一个旨在执行此类操作的库.在标准库中,Python具有模块. >

文档中提供了有关使用curses的很好的教程.这也建议Windows用户使用控制台模块.

I am learning Python, and I have decided to make some practise by coding a mini chat in terminal, with a server and a client. But there is a problem, to make a real chat, the client must be able to type, send and receive messages in the same time like that:

//MESSAGES
>bla bla to send

So messages are scrolling while in the bottom of the shell, there's my input.

I have searched the web how to make that, maybe the multi-threading, no. The input() fonction blocks the process.

If you have an answer to my question, it'll be great! Thanks in advance!

解决方案

On Unix systems, you might want to check out curses, which is a library designed to do this type of thing. Python has a module for it in the standard library.

There is a good tutorial on using curses in the docs. This also suggests the console module for Windows users.

这篇关于同时使用input()和print()(例如在聊天中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆