python - 在控制台的标题中放置一个变量(Python)
问题描述
我想在控制台窗口的标题中放置一个变量,但是一旦我运行此代码,控制台就会崩溃:
import ctypes
for i in range(1500000000):
ctypes.windll.kernel32.SetConsoleTitleW("StackOverFlow - HELP -[" + i + "]")
解决方案
如果与字符串连接,则该变量i
只能是字符串。此语法告诉 python 尝试转换i
为字符串并将其放置在%s
.
ctypes.windll.kernel32.SetConsoleTitleW("StackOverFlow - HELP -[%s]" % i)
或者
ctypes.windll.kernel32.SetConsoleTitleW(f"StackOverFlow - HELP -[{i}]")
如果您使用的是 python3.6 及更高版本。或者
ctypes.windll.kernel32.SetConsoleTitleW("StackOverFlow - HELP -[{}]".format(i))
Kinda打破了python的禅宗:
应该有一种——最好只有一种——明显的方法来做到这一点。
推荐阅读
- javascript - 保存的图像说无效图像。这段代码有什么问题?
- python - 使用 PyQt5 获取滑块值
- java - .project 文件、.settings 文件夹、bin 文件夹在意外导入 IntelliJ Maven 后继续在 VSCode 中自动生成
- r - R中没有发现符号变化错误但不是excel
- java - 从多命名空间 XML 中删除命名空间的数据
- docker - 自动从 php fpm 镜像启动 Nginx
- matlab - MATLAB 表 - 选择满足条件的第一行
- windows - 在 Get cmdlet 中过滤掉空参数值
- mongodb - 无法从远程服务器导出 MongoDB 数据库
- symfony - 如何将 Bootstrap 添加到我的 Symfony 4.1 项目中?