見出し画像

Pythonで作るGUIアプリ入門 Part11 -ウィジェットの各種設定-

ウィジェットの各種設定

GUI ウィンドウ内のウィジェットを以下のように各種設定出来ます。

  • テキストの色設定

  • フォントの設定

  • テキストエリアのサイズ設定

  • テキストエリアの背景色設定

  • 位置の設定

  • 表示・非表示設定

  • イベント開始設定

今回は、よく使用するテキストの設定を紹介していきます。

テキストの色設定

テキストの色は、sg.Text(text = "", text_color = ‘16進数カラーコード ‘) で指定できます。16進数カラーコードは、#と6桁の数字で色を表します。HTMLやCSSでも色を指定するときに使用しますので、馴染み深い方も多いと思います。

GUIウィンドウに4つのテキストエリアを設置し、それぞれ異なるカラーコードを設定した図とコードは以下の通りとなります。

図11-1
import PySimpleGUI as sg

layout = [
    [sg.Text('オリジナルテキスト')],
    [sg.Text('テキスト1',  text_color='#008000')],
    [sg.Text('テキスト2',  text_color='#000000')],
    [sg.Text('テキスト3',  text_color='#ff0000')],
]

window = sg.Window('テキストカラーの設定', layout)

while True:
    event, value = window.read()  # イベントの入力を待つ

    if event is None:
        break

window.close()

テキストのフォント設定

テキストのフォントは、sg.Text(text = "", font = (‘フォント名‘, フォントサイズ)) で指定できます。フォントは、Tkinterのフォントファミリーを使用出来ます。使用なフォントを調べるには、以下のスクリプトを実行して下さい(コピペでOKです)。

import tkinter
import tkinter.font

tkinter.Tk()
print(tkinter.font.families())

ターミナルにフォント一覧が表示されますので、好みのフォントを指定します(Macでのフォント一覧は、本記事の一番最後に掲載しております)。今回は YuGothic を使用します。

先ほどのコードに、フォントとフォントサイズを追加すると以下のようになります。コードについては、レイアウトエリアのみを掲載します。

図11-2
layout = [
    [sg.Text('オリジナルテキスト')],
    [sg.Text('テキスト1', text_color='#008000', font=('YuGothic', 20))],
    [sg.Text('テキスト2', text_color='#000000', font=('YuGothic', 30))],
    [sg.Text('テキスト3', text_color='#ff0000', font=('YuGothic', 40))],
]

テキストエリアのサイズ設定

テキストエリアのサイズは、sg.Text(text = "", size = (文字数,行数 )) で指定できます。文字数は、半角での文字数となります。全角は2文字扱いとなります。

先ほどのコードに、テキストエリアのサイズを追加すると以下のようになります。コードについては、レイアウトエリアのみを掲載します。

図11-3
layout = [
    [sg.Text('オリジナルテキスト')],
    [sg.Text('テキスト1', text_color='#008000', font=('YuGothic', 20),size=(10,2))],
    [sg.Text('テキスト2', text_color='#000000', font=('YuGothic', 30),size=(4,2))],
    [sg.Text('テキスト3', text_color='#ff0000', font=('YuGothic', 40))],
]

テキストエリアの背景色設定

テキストエリアの背景色は、sg.Text(text = "", background_color=‘16進数カラーコード ‘) で指定できます。

先ほどのコードに、テキストエリアの背景色を追加すると以下のようになります。コードについては、レイアウトエリアのみを掲載します。

図11-4
layout = [
    [sg.Text('オリジナルテキスト')],
    [sg.Text('テキスト1', text_color='#008000', font=('YuGothic', 20),size=(10,2))],
    [sg.Text('テキスト2', text_color='#000000', font=('YuGothic', 30),size=(4,2))],
    [sg.Text('テキスト3', text_color='#ff0000', font=('YuGothic', 40),background_color='#ffffff')],
]


以上のようにして、テキストウィジェットを各種設定出来ます。

次回もウィジェットの設定の続きを説明します。

GUIアプリ入門講座は毎日更新します!
記事が参考になりましたら、❤️を押していただけると励みになります😊


'Academy Engraved LET', 'Al Bayan', 'Al Nile', 'Al Tarikh', 'American Typewriter', 'Andale Mono', 'Apple Braille', 'Apple Chancery', 'Apple Color Emoji', 'Apple LiGothic', 'Apple LiSung', 'Apple SD Gothic Neo', 'Apple Symbols', 'AppleGothic', 'AppleMyungjo', 'Arial', 'Arial Black', 'Arial Hebrew', 'Arial Hebrew Scholar', 'Arial Narrow', 'Arial Rounded MT Bold', 'Arial Unicode MS', 'Avenir', 'Avenir Next', 'Avenir Next Condensed', 'Ayuthaya', 'Baghdad', 'Bangla MN', 'Bangla Sangam MN', 'Baoli SC', 'Baoli TC', 'Baskerville', 'Beirut', 'BiauKai', 'Big Caslon', 'BM Kirang Haerang', 'Bodoni 72', 'Bodoni 72 Oldstyle', 'Bodoni 72 Smallcaps', 'Bodoni Ornaments', 'Bradley Hand', 'Brush Script MT', 'Chalkboard', 'Chalkboard SE', 'Chalkduster', 'Charter', 'Cochin', 'Comic Sans MS', 'Copperplate', 'Corsiva Hebrew', 'Courier', 'Courier New', 'Damascus', 'DecoType Naskh', 'Devanagari MT', 'Devanagari Sangam MN', 'Didot', 'DIN Alternate', 'DIN Condensed', 'Diwan Kufi', 'Diwan Thuluth', 'Euphemia UCAS', 'Farah', 'Farisi', 'Futura', 'Galvji', 'GB18030 Bitmap', 'Geeza Pro', 'Geneva', 'Georgia', 'Gill Sans', 'Grantha Sangam MN', 'Gujarati MT', 'Gujarati Sangam MN', 'GungSeo', 'Gurmukhi MN', 'Gurmukhi MT', 'Gurmukhi Sangam MN', 'Hannotate SC', 'Hannotate TC', 'HanziPen SC', 'HanziPen TC', 'HeadLineA', 'Hei', 'Helvetica', 'Helvetica Neue', 'Herculanum', 'Hiragino Sans CNS', 'Hiragino Sans GB', 'Hoefler Text', 'Impact', 'InaiMathi', 'ITF Devanagari', 'ITF Devanagari Marathi', 'Kai', 'Kailasa', 'Kaiti SC', 'Kaiti TC', 'Kannada MN', 'Kannada Sangam MN', 'Kefa', 'Khmer MN', 'Khmer Sangam MN', 'Kohinoor Bangla', 'Kohinoor Devanagari', 'Kohinoor Gujarati', 'Kohinoor Telugu', 'Kokonor', 'Krungthep', 'KufiStandardGK', 'Lantinghei SC', 'Lantinghei TC', 'Lao MN', 'Lao Sangam MN', 'Libian SC', 'Libian TC', 'LiHei Pro', 'LingWai SC', 'LingWai TC', 'LiSong Pro', 'Lucida Grande', 'Luminari', 'Malayalam MN', 'Malayalam Sangam MN', 'Marker Felt', 'Menlo', 'Microsoft Sans Serif', 'Mishafi', 'Mishafi Gold', 'Monaco', 'Mshtakan', 'Mukta Mahee', 'Muna', 'Myanmar MN', 'Myanmar Sangam MN', 'Nadeem', 'Nanum Brush Script', 'Nanum Gothic', 'Nanum Myeongjo', 'Nanum Pen Script', 'New Peninim MT', 'Noteworthy', 'Noto Nastaliq Urdu', 'Noto Sans Kannada', 'Noto Sans Myanmar', 'Noto Sans Oriya', 'Noto Serif Myanmar', 'Optima', 'Oriya MN', 'Oriya Sangam MN', 'Osaka', 'Palatino', 'Papyrus', 'Party LET', 'PCMyungjo', 'Phosphate', 'PilGi', 'PingFang HK', 'PingFang SC', 'PingFang TC', 'Plantagenet Cherokee', 'PSL Ornanong Pro', 'PT Mono', 'PT Sans', 'PT Sans Caption', 'PT Sans Narrow', 'PT Serif', 'PT Serif Caption', 'Raanana', 'Rockwell', 'Sana', 'Sathu', 'Savoye LET', 'Shree Devanagari 714', 'SignPainter', 'Silom', 'SimSong', 'Sinhala MN', 'Sinhala Sangam MN', 'Skia', 'Snell Roundhand', 'Songti SC', 'Songti TC', 'STFangsong', 'STHeiti', 'STIXGeneral', 'STIXIntegralsD', 'STIXIntegralsSm', 'STIXIntegralsUp', 'STIXIntegralsUpD', 'STIXIntegralsUpSm', 'STIXNonUnicode', 'STIXSizeFiveSym', 'STIXSizeFourSym', 'STIXSizeOneSym', 'STIXSizeThreeSym', 'STIXSizeTwoSym', 'STIXVariants', 'STKaiti', 'STSong', 'Sukhumvit Set', 'Symbol', 'Tahoma', 'Tamil MN', 'Tamil Sangam MN', 'Telugu MN', 'Telugu Sangam MN', 'Thonburi', 'Times', 'Times New Roman', 'Trattatello', 'Trebuchet MS', 'Verdana', 'Waseem', 'Wawati SC', 'Wawati TC', 'Webdings', 'Weibei SC', 'Weibei TC', 'Wingdings', 'Wingdings 2', 'Wingdings 3', 'Xingkai SC', 'Xingkai TC', 'Yuanti SC', 'Yuanti TC', 'Yuppy SC', 'Yuppy TC', 'Zapf Dingbats', 'Zapfino', 'Klee', 'Hiragino Sans', 'Hiragino Maru Gothic ProN', 'Hiragino Mincho ProN', 'Heiti SC', 'Heiti TC', 'Tsukushi A Round Gothic', 'Tsukushi B Round Gothic', 'Toppan Bunkyu Gothic', 'Toppan Bunkyu Midashi Gothic', 'Toppan Bunkyu Midashi Mincho', 'Toppan Bunkyu Mincho', 'YuGothic', 'YuKyokasho', 'YuKyokasho Yoko', 'YuMincho', 'YuMincho +36p Kana'

Tkinterフォント一覧


この記事が気に入ったらサポートをしてみませんか?