0
0

More than 1 year has passed since last update.

Windows/Mac/Linux対応のウインドウタイトルを設定する関数を作る

Last updated at Posted at 2022-10-29

タイトルバーに表示する文字列を設定する関数です。
Windowsならコマンドプロンプト・PowerShell、Mac・Linuxならターミナル(端末)のウインドウ上部のタイトルバーに表示されている文字列をいじります。

私の調べる限りだとタイトルを設定する関数がおそらく存在しないっぽいので、自分で作ってやろうって感じです。

環境

  • Python - すべて3.10.xに統一

今回は以下の4つのOSで動作検証を行いました。

  • Windows - Windows11 Pro 22H2
  • Mac - macOS Ventura 13.0
  • Linux - Ubuntu 22.04.1 LTS / Fedora Linux 36

ソースコード

Title.py
import os

def title(text):
    # OSの種類を判別する
    # Windows
	if os.name == 'nt':
		os.system(f'title {text}')
    # Mac / Linux
	elif os.name == 'posix':
		print(f'\x1b]2;{text}\x07', end='', flush=True)

想像してたより結構シンプルに仕上がりました。内容としては

  1. OSの種類を判別する
  2. それぞれのOSに合ったタイトルを設定する命令を実行する

この2ステップのみです。

動かしてみた

Windows

Windows11 Pro 22H2
image.png

Mac

macOS Ventura 13.0
スクリーンショット 2022-10-29 14.38.38.png

Linux

Ubuntu 22.04.1 LTS
image.png

Fedora Linux 36
image.png

参考にさせていただいたサイト

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0