2014-04-13 2 views
4

Gretings!ttk.Notebook의 "tab header"색상 변경

ttk.Notebook을 사용하여 만든 탭 머리글에 표시된 색을 변경하고 싶습니다. 잠시 동안 검색 한 결과, ttk 위젯의 스타일을 변경한다는 것을 알았습니다. ttk를 사용할 수 있습니다. 왜냐하면 노트북에는 색상을 변경하는 구성 옵션이 없기 때문입니다. 그러나, 나는 BackgroundBook과 NoteBook 객체의 전경을 변경하는 방법을 찾았지 만, 배경이 흰색 (선택된 경우)이거나 회색 (선택되지 않은 경우) 인 "탭 머리글"을 구성하는 방법이 아닙니다.

아무도 도움이 될 수 있습니까?

내가 미리

import Tkinter as tki 
import ttk 

... 
##Other code. Not relevant here 
... 

#create tabs and associate the apropriate frames to it 
tabs = ttk.Notebook(parent.master) 
ttk.Style().configure("TNotebook", background=mainWcolor, foreground='green') #configure "tabs" background color 

paramsFrame = tki.Frame(tabs, bg=mainWcolor) #frame with control parameters 
comsFrame = tki.Frame(tabs, bg=mainWcolor)  #frame with communication parameters. 
ssInfoFrame = tki.Frame(tabs, bg=mainWcolor) #frame with start and stop messages and procedures 

tabs.add(paramsFrame, text = "Control") 
tabs.add(comsFrame, text = "Communications") 
tabs.add(ssInfoFrame, text = "Start & Stop info") 
tabs.pack() 

감사 할 노력하고있어 관련 내가 지금 가지고있는 코드입니다.

답변

7

사용자 지정 테마를 만들 수 있습니다.

import tkinter as tk 
from tkinter import ttk 

root = tk.Tk() 

mygreen = "#d2ffd2" 
myred = "#dd0202" 

style = ttk.Style() 

style.theme_create("yummy", parent="alt", settings={ 
     "TNotebook": {"configure": {"tabmargins": [2, 5, 2, 0] } }, 
     "TNotebook.Tab": { 
      "configure": {"padding": [5, 1], "background": mygreen }, 
      "map":  {"background": [("selected", myred)], 
          "expand": [("selected", [1, 1, 1, 0])] } } }) 

style.theme_use("yummy") 

note = ttk.Notebook(root) 
f1 = ttk.Frame(note, width=300, height=200) 
note.add(f1, text = 'First') 
f2 = ttk.Frame(note, width=300, height=200) 
note.add(f2, text = 'Second') 
note.pack(expand=1, fill='both', padx=5, pady=5) 

tk.Button(root, text='yummy!').pack(fill='x') 

root.mainloop() 

편집

가장 상세한 TTK 문서는 TCL/TK 세계

예에서입니다. 유용한 파이썬 기반의 예를 들어

http://www.tcl.tk/man/tcl/TkCmd/ttk_notebook.htm

, 당신은 내가 몇 시간 동안 망각의 답변을 사용하고있었습니다 http://code.google.com/p/python-ttk/

+0

위대한 작품! 감사! 그러나 코드가 약간 복잡하다는 것을 알고 있습니다. 당신은 내 질문에 대답했지만, 지금 나는 당신이 실제로 한 일을 이해하는 데 관심이 생겼습니다. 만약 그렇다면, 당신은 노트 위젯을 다루는 데 유용한 모든 문서를이 수준에서 보았습니까? 내가 찾은 자습서는이 수준의 세부 사항을 설명하지 않습니다. –

+0

ttk 스타일 정보의 또 다른 적절한 소스는 http://www.tkdocs.com/tutorial/styles.html –

+0

감사의 망각과 브라이언입니다! –

3

에서 pyttk-샘플 패키지 를 잡을 수 있지만, 나는 어디에 개방 문제가 발생/저장 대화 상자 버튼 외곽선이 사라지고 Text 위젯의 Checkbutton이 체크되지 않은 것으로 나타납니다 (체크 된 경우에도 마찬가지 임). 그래서, 나는 테마 코드를 어떤 스타일 구성으로 변환하여 문제를 해결했습니다 (해결했습니다). 이렇게하면 탭 막대 색상, 탭 배경/전경 및 활성 탭 배경/전경을 변경할 수 있습니다. 또한 선택한 테마의 나머지 부분에 문제가 발생하지 않습니다. 그것은 본질적으로 번역 된 주제의 코드와 같습니다. 그래서, 정말로, 망각은 신용의 대부분을받을 자격이 있습니다.

Style().configure("TNotebook", background=myTabBarColor); 
Style().map("TNotebook.Tab", background=[("selected", myActiveTabBackgroundColor)], foreground=[("selected", myActiveTabForegroundColor)]); 
Style().configure("TNotebook.Tab", background=myTabBackgroundColor, foreground=myTabForegroundColor); 

편집 : 명백히이 솔루션은 Windows에서 작동하지 않습니다. Linux (Xubuntu의 여러 버전)에서 테스트했습니다.

+1

위의 내용이 Windows에서 작동하지 않습니다. 전경색 만 보았지만 배경색은 보지 못했습니다. 나는 왜 위의 작동하지 않는 설명이 게시 (https://stackoverflow.com/questions/22389198/ttk-styling-tnotebook-tab-background-and-borderwidth-not-working) 발견하고 그것을 허용했다. 작동 시키십시오. – Gary02127

+1

@ Gary02127 정말 잘 알고 있습니다. Linux (여러 버전의 Xubuntu)에서이 코드를 사용했습니다. – Shule