2017-03-21 2 views
0

ttk 'clam'テーマを使用するボタンと同じ外観を持つボタンウィジェットのカスタムスタイルを作成する必要があります。'clam'と同じカスタムttkスタイルを作成するttkテーマ(ボタンウィジェット固有)

私のようなテーマを設定することができます。しかし、テーマの性質を考えると、これはその後、「貝」を使用するようにすべてのTTKのウィジェットを設定します

s = ttk.Style() 
s.theme_use('clam') 

私は、クラムの外観とデフォルトのttkを使用する他のttkボタンを設定できるようにしたいと思います。

私は「TButton」のレイアウトと構成を見てみましたが、clamテーマは使用されていますが、テーマはスタイルのコレクションで、カスタムスタイルベースの「マップ」方法は不明ですクラムボタンのスタイル。

答えて

1

このコードを使用:

import Tkinter as tk 
import ttk 

def get_element_details(style): 
    print('element: %s' % style) 
    print('option: %s' % str(s.element_options(style))) 
    layout = s.layout(style) 
    for elem, elem_dict in layout: 
     get_sub_element_details(elem, elem_dict) 
    print(layout) 

def get_sub_element_details(elem, _dict, depth=1): 
    print('%selement: %s' % (''.join(['\t' for i in range(depth)]), elem)) 
    for key in _dict: 
     if key != 'children': 
      print('%s%s: %s' % (''.join(['\t' for i in range(depth+1)]), key, _dict[key])) 
    print('%soption: %s' % (''.join(['\t' for i in range(depth+1)]), s.element_options(elem))) 
    if 'children' in _dict: 
     for child, child_dict in _dict['children']: 
      get_sub_element_details(child, child_dict, depth+1) 

root = tk.Tk() 
widget = ttk.Button(root, text='test') 
widget.grid(sticky='nesw') 

style = widget.winfo_class() 

s = ttk.Style() 

print(s.theme_use()) 
print('normal theme') 
get_element_details(style) 

print('\nclam theme') 
s.theme_use('clam') 
get_element_details(style) 

あなたは、ウィジェットのすべてのレイアウトと設定オプションについての詳細はEGTすることができます。私のボックス(XP)上のネイティブテーマに 私はこの出力を得る:

element: TButton 
option:() 
    element: Button.button 
     sticky: nswe 
     option:() 
     element: Button.focus 
      sticky: nswe 
      option:() 
      element: Button.padding 
       sticky: nswe 
       option: ('-padding', '-relief', '-shiftrelief') 
       element: Button.label 
        sticky: nswe 
        option: ('-compound', '-space', '-text', '-font', '-foreground', '-underline', '-width', '-anchor', '-justify', '-wraplength', '-embossed', '-image', '-stipple', '-background') 

と私が得るハマグリをテーマにした:アサリのテーマはオプションでButton.border要素を持っていることを

element: TButton 
option:() 
    element: Button.border 
     border: 1 
     sticky: nswe 
     option: ('-bordercolor', '-lightcolor', '-darkcolor', '-relief', '-borderwidth') 
     element: Button.focus 
      sticky: nswe 
      option: ('-focuscolor', '-focusthickness') 
      element: Button.padding 
       sticky: nswe 
       option: ('-padding', '-relief', '-shiftrelief') 
       element: Button.label 
        sticky: nswe 
        option: ('-compound', '-space', '-text', '-font', '-foreground', '-underline', '-width', '-anchor', '-justify', '-wraplength', '-embossed', '-image', '-stipple', '-background') 

ノート、ネイティブテーマはオプションなしのButton.button要素を持っています。

あなたはclamテーマからレイアウトを保存することができます(書き込み時、またはclamテーマを読み込んでレイアウトを取得し、テーマを元に戻してレイアウトを読み込むことで実行時に取得できます)ボタン。これは動作するはずです理論的には

EDIT :

import Tkinter as tk 
import ttk 

root = tk.Tk() 

style = 'TButton' 

s = ttk.Style() 

#s.theme_use('clam') 

#get_element_details(style) 

clambuttonlayout = [('Button.border', {'border': '1', 'children': [('Button.focus', {'children': [('Button.padding', {'children': [('Button.label', {'sticky': 'nswe'})], 'sticky': 'nswe'})], 'sticky': 'nswe'})], 'sticky': 'nswe'})] 

s.layout('clam.TButton', clambuttonlayout) 

b1 = ttk.Button(root, text="Button 1", style='clam.TButton') 
b1.grid() 
b2 = ttk.Button(root, text="Button 2", style='TButton') 
b2.grid() 

root.mainloop() 

しかし、私はこれを行うときにテキストは、もはや最初のボタンの上に表示されるいくつかの理由で... 私はそれを把握する場合、私は再び編集します。

+0

こんにちはJames。ご回答有難うございます!あなたのprintステートメントを見てPython x3を使っていると仮定していますか?あなたのコードを実行すると、 'print '%sption:%s'%( '' \ t '(範囲'(深さ+1)s) ')、s.element_options(elem) ) AttributeError: 'int'オブジェクトの属性が 'element_options''ではありません - これはPy 3xやTkinterの別のバージョンによるものでしょうか? –

+0

申し訳ありませんが、python2とpython3の違いは、インデックスとしてsを使用すると、関数内のスタイルオブジェクトを上書きしました。私は今私に変更してpython2.7でテストしました。 –

+0

ありがとうJames thats really cool! - 上の出力を使用してボタンを手作業でボタンに合わせる方法を詳しく説明できますか? –

関連する問題