2014-09-16 2 views
2

파이썬에서 그놈 터미널의 창 제목을 어떻게 설정할 수 있습니까?파이썬에서 그놈 터미널 창 제목을 설정 하시겠습니까?

다른 터미널에서 여러 개의 python 스크립트를 실행하고 있습니다. 한 번 실행 된 python 스크립트가 자동으로 스크립트 내에서 수정할 수있는 상태 텍스트의 창 제목을 설정하고 싶습니다.

+0

당신이 터미널을 실행하는 데 사용하는 코드를 게시 할 수 :

나는 또한 그들이 OSC 이스케이프 코드를 지원에도 불구하고 어떤 단말기가 확인 실패 때문에 않는 다른 대답 같은 if os.environ['TERM'] == 'xterm'을 확인하지 않는 것이 좋습니다? 전체 세션에 대해 변경하거나 정적으로 유지 하시겠습니까? –

+0

지금 시도 할 수는 없지만 [편도] (http://askubuntu.com/questions/30988/how-do-you-set-the-title-of-the-active-gnome-terminal)를 선택하십시오. -from-the-command-line) 파이썬을 사용하지 않고 수행 한 다음 [외부 명령 호출] (http://stackoverflow.com/questions/89228/calling-an-external-command-in-python?rq= 1). – fredtantini

답변

2

당신은 XTerm control sequence를 사용할 수 있습니다

print(b'\33]0;title you want\a') 

참고 : 문 위에 추가로 줄 바꿈을 인쇄 할 수 있습니다. 이를 방지하기 위해, sys.stdout.write 사용

import sys 
sys.stdout.write(b'\33]0;title you want\a') 
sys.stdout.flush() 

파이썬 3.x의에서 :

print('\33]0;title you want\a', end='') 
sys.stdout.flush() 

를 3.3 이상 파이썬에서 :

print('\33]0;title you want\a', end='', flush=True) 

또는

sys.stdout.buffer.write(b'\33]0;title you want\a') 
sys.stdout.buffer.flush() 
0

falsetru's answer에 추가 , Python (2 and 3) al

import os 
import sys 

def set_xterm_title(title='Python %d.%d.%d' % sys.version_info[:3]): 
    ''' 
    Set XTerm title using escape sequences. 
    By default, sets as 'Python' and the version number. 
    ''' 
    sys.stdout.write('\33]0;' + title + '\a') 
    sys.stdout.flush() 

# Make sure this terminal supports the OSC code (\33]), 
# though not necessarily that it supports setting the title. 
# If this check causes compatibility issues, you can add 
# items to the tuple, or remove the check entirely. 
if os.environ.get('TERM') in (
     'xterm', 
     'xterm-color', 
     'xterm-256color', 
     'linux', 
     'screen', 
     'screen-256color', 
     'screen-bce', 
     ): 
    set_xterm_title() 
1

허용 대답했다 : 그래서 표준 출력에 일반 문자열을 작성 지원 : 파이썬 쉘을 열 때

import sys 
sys.stdout.write('\33]0;title you want\a') 
sys.stdout.flush() 

을 그건 그렇고, 내가 제목을 설정하는 내 ~/.pythonstartup 파일에 넣고 Python3에서는 잘못되었습니다. 이것은 Python> = 3.6에서 작동합니다 :

terminal_title = "title you want" 
print(f'\33]0;{terminal_title}\a', end='', flush=True) 

flush은 필수입니다. 의견보기.

[[email protected] ~]$ echo $TERM 
xterm-256color 
[[email protected] ~]$ echo $TERM_PROGRAM 
iTerm.app 
+1

난 대답을 보지까지 파이썬 3.x에 대한 작동하지 않는 코드를 게시하지 않았다. 고맙습니다. – falsetru

+1

@falsetru 당신이 고친 print (b ") 외에도, 이것은 놀랍게 까다 롭습니다 :'stdout.flush()'는 sys.stdout을 위해 절대적으로 필요하고'flush = True'는 print()를 위해 필요합니다. 그것 없이는'\ n'마다 stdout을 자동으로 플러시하는 디버깅 print() 문을 제거하자마자 코드가 작동하지 않게되었습니다. 파이썬/libc의 자동 플러시를 트리거하지 않는 작은 제목을 설정하려고하면 같은 이유로 자동으로 응답이 끊어집니다. – Navin

+1

답변을 한 번 더 업데이트했습니다. 다시 감사합니다. – falsetru

관련 문제