Python Code Notes

Information by python engineer

【tkinter.ttk】Notebookの使い方【Python】

f:id:vigilantPotato:20210627022313p:plain
Notebookウィジェットを使用することで、タブでページを切り替える画面を設定することができる。

目次

  1. Notebookの概要
  2. 複数のウィジェットを表示する場合
  3. オプション・メソッド

【スポンサーリンク】


Notebookの概要

Notebookウィジェットを用いて、EntryとButtonウィジェットをタブで分けて表示する例を示す。

import tkinter
from tkinter import ttk

root = tkinter.Tk()

#Notebook
note = ttk.Notebook(root)

#Entry
text = tkinter.Entry(root)

#Button
button = tkinter.Button(
    root,
    text="button",
    )

#notebookに追加
note.add(text, text="Entry")    #Entryを表示
note.add(button, text="Button") #Buttonを表示

note.pack()

root.mainloop()

addメソッドでタブにウィジェットを設定し、textオプションでタブのタイトルを設定している。実行すると以下の画面が表示され、タブのクリックで表示を切り替えることができる。

f:id:vigilantPotato:20210627021949p:plain
f:id:vigilantPotato:20210627022007p:plain

[↑ 目次へ]


【スポンサーリンク】


複数のウィジェットを表示する場合

複数のウィジェットを一つのタブ内に表示させる場合は、Frameウィジェットを使用する。以下、EntryとButtonを一つのタブに表示する例を示す。

import tkinter
from tkinter import ttk

root = tkinter.Tk()

#Notebook
note = ttk.Notebook(root)

#Frame
f1 = tkinter.Frame(root)

#Entry
text = tkinter.Entry(f1)    #f1を指定
text.pack()

#Button
button = tkinter.Button(
    f1,                     #f1を指定
    text="button",
    )
button.pack()

#notebookに追加
note.add(f1, text="Frame")  #f1を表示
note.pack()

root.mainloop()

Frameウィジェットを使用することで、複数のウィジェットを一つのタブ内に表示させている。実行すると、一つのタブの中にEntryとButtonが表示される。

f:id:vigilantPotato:20210627022119p:plain

[↑ 目次へ]


【スポンサーリンク】


オプション・メソッド

オプション・メソッド 動作
height 高さの設定
width 幅の設定
.add(widget, *options) タブの追加
.forget(tab_id) タブの削除
.hide(tab_id) タブの非表示

addメソッドのオプション

オプション 動作
state タブの表示設定 "normal", "disable", "hidden"から選択可能
text タブに表示する文字列
image タブに表示する画像

[↑ 目次へ]


【スポンサーリンク】