memopy

pythonで作ってみました的なブログ

python tkinter 色(color)の設定方法

python tkinter 色(color)の設定方法

python tkinterにおける色(color)の設定方法がいくつかあるので整理する。
参考ページ(英語):http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/colors.html
f:id:memopy:20170611091342p:plain

RGBの指定による方法

#rgb 4bit 16色
#rrggbb 8bit 256色
#rrrgggbbb 16bit 4096色
指定の例

※python3で作成した(python2で使用するには、モジュール名をTkinterとする)

import tkinter as tk 

root = tk.Tk()
# 色は、背景色は、bgオプションに設定する
button1 = tk.Button(root,text="#fff",bg="#fff")
button1.pack(fill="x")
button2 = tk.Button(root,text="#ff0000",bg="#ff0000")
button2.pack(fill="x")
button3 = tk.Button(root,text="#000fff000",bg="#000fff000")
button3.pack(fill="x")
button4 = tk.Button(root,text="#00ffff",bg="#00ffff")
button4.pack(fill="x")

root.mainloop()

f:id:memopy:20170611091342p:plain

色名称による設定

  • 全ての環境において使用可能な色名称

'white', 'black', 'red', 'green', 'blue', 'cyan', 'yellow', 'magenta'

  • その他の色名称は、ローカル環境による(おそらく使用可能)

f:id:memopy:20170611091941p:plain
※上記GUIソースコードは下記のページ(英語)に公開されていた。
https://stackoverflow.com/questions/4969543/colour-chart-for-tkinter-and-tix-using-python