LoginSignup
5
5

More than 5 years have passed since last update.

Tkinterの色指定で使用可能な値

Posted at

Tkinterで色指定する場面がけっこうある。

frame = Tkinter.Frame(bg='red')

この例では、 bg でフレームの背景色を red という文字列リテラルで指定している
が、使用可能な値に何があるのか、気になったので調べてみた。

確認に使った環境

  • Windows XP Professional 32bit (SP3)
  • Python 2.7.3

環境依存もあるんじゃないのという気はしてるので、参考程度で。

数値による指定

4ビット/8ビット/12ビットの数値形式の文字列で指定可能。

Tkinter.Frame(bg='#fff') # white
Tkinter.Frame(bg='#000000') # black
Tkinter.Frame(bg='#000fff000') # green

名前による指定

redyellow といった名前で指定可能。ここで使える値に何があるかについては、
Tkinterのラッパー元(?) になってるTcl/Tkのサイトにドキュメントがあった。

ここに書かれている752種類の色指定用の文字列は、すべて使用可能であった。

大文字・小文字の違いは問題ないらしい。

class TkinterColorsTestCase(unittest.TestCase):

    def test_(self):

        def _assert_color(color):
            try:
                for _color in [
                        # 元の値
                        color, 
                        # 大文字
                        color.upper(), 
                        # 小文字
                        color.lower(),
                        ]:
                    Frame(bg=_color)
            except TclError as e:
                self.assertTrue(False, msg=color)

        map(_assert_color, Colors)

if __name__ == '__main__':
    unittest.main()

色の名称で標準化されてるのかな。ちょっと見つけられてないけど。

5
5
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
5
5