python - 更改背景颜色和文本颜色
问题描述
假设我在列表中有几个数字:
listt = [2, 4, 6]
有什么方法可以改变每个数字的背景颜色和颜色,具体取决于它是哪个数字。例如:
for i in range(3):
if listt[i] == 2:
# make background color green and make number red
elif listt[i] == 4:
# make background color orange and make number green
elif listt[i] == 6
# make background color red and make number orange
print(nlistt[i])
有什么办法可以做到这一点,如果没有背景和常规颜色,你可以做 2 个中的 1 个。这应该在控制台中,而不是在 pygame 之类的新窗口中。
解决方案
只需将颜色代码应用于您要打印的内容,颜色代码可以在@Alex Taylor提到的帖子中找到。
listt = [2, 4, 6]
nlistt = listt.copy()
for i in range(3):
if listt[i] == 2:
# make background color green and make number red
nlistt[i] = '\033[1;31;42m' + str(nlistt[i]) + '\033[0m'
elif listt[i] == 4:
# make background color orange and make number green
nlistt[i] = '\033[1;32;43m' + str(nlistt[i]) + '\033[0m'
elif listt[i] == 6:
# make background color red and make number orange
nlistt[i] = '\033[1;33;41m' + str(nlistt[i]) + '\033[0m'
print(nlistt[i])
推荐阅读
- cassandra - 可扩展的 Janusgraph 组件
- rest - 我在使用 Nginx reverse_proxy 连接到我的 Rest-API 时收到 404
- html - 如何获取网页中所有元素的堆叠顺序?
- javascript - 如何在 Vue.js 中绑定整个组件?
- python - 使用python重命名文件夹中的多个文件,其中包含来自文本文件的新名称列表
- react-native - 组件上的 React Native ref/reference 不起作用
- flutter - 飞镖中的日期时间比较
- android - 拍照后如何通过蓝牙将图像从图像视图发送到电脑?
- flutter - 在画布上从相机中绘制照片?
- java - 如何以一种智能的方式(以及超过 100 个项目)创建这个 Android 自定义“复选框”,如选择?