LoginSignup
0
1

More than 1 year has passed since last update.

Tkinter 複数のフレームを切り替えながらチェックボックスを使いチェック値を取得する

Last updated at Posted at 2022-06-11

構造と仕組み

メインウィンドウ
├ page1フレーム ┬ page2ボタン
│       └ checkboxボタン

├ page2フレーム ┬ page1ボタン
│       └ checkboxボタン

└ checkboxフレーム ─ tmpボタン

page1とpage2とcheckboxは自由に行き来できる
checkboxボタンを押した時に、現在のフレームをtmpへ保存しておき処理が終わったらtmpを参照して元のフレームへ戻る。
tmpボタンが押されたときに、checkboxでチェックした値をcheck_resultへ格納して結果を表示する(このコードは表示したの後に初期化しているが、実際は、この値を自由に使えばよい)

コード

import tkinter as tk


class App():
    def __init__(self, root: tk.Tk):

        self.root = root
        self.root.geometry('800x800')

        # 現在のフレーム
        self.current_frame = None

        # フレーム一時保存用
        self.tmp_frame = None

        # ===========
        # 作成
        # ===========

        # ページフレーム
        page_frame1 = tk.Frame(self.root, bg='yellow', width=1000, height=1000)
        page_frame2 = tk.Frame(self.root, bg='orange', width=1000, height=1000)
        self.current_frame = page_frame1

        # チェックフレーム
        self.check_frame = tk.Frame(self.root, bg='green', width=1000, height=1000)
        # チェックしたかどうか
        self.check_val = {}
        # チェック結果を格納する
        self.check_result = []

        # ボタン
        # ページ2 to ページ1
        switch_page1 = tk.Button(page_frame2, text='switch page1', command=lambda: self.switch_page_frame(page_frame2, page_frame1))
        # ページ1 to ページ2
        switch_page2 = tk.Button(page_frame1, text='switch page2', command=lambda: self.switch_page_frame(page_frame1, page_frame2))
        # チャックフレームへ切り替える
        switch_check_frame_btn1 = tk.Button(page_frame1, text='switch check', command=self.switch_check_frame)
        switch_check_frame_btn2 = tk.Button(page_frame2, text='switch check', command=self.switch_check_frame)
        # tmpフレームへ切り替える
        switch_tmp_frame_btn = tk.Button(self.check_frame, text='switch tmp', command=self.switch_tmp_frame)

        # ===========
        # 配置
        # ===========

        # フレーム
        self.root.rowconfigure(0, weight=1)
        self.root.columnconfigure(0, weight=1)
        page_frame1.grid(row=0, column=0, sticky=tk.NSEW)
        page_frame2.grid(row=0, column=0, sticky=tk.NSEW)
        self.check_frame.grid(row=0, column=0, sticky=tk.NSEW)

        # ボタン
        switch_page1.pack()
        switch_page2.pack()
        switch_check_frame_btn1.pack()
        switch_check_frame_btn2.pack()
        switch_tmp_frame_btn.pack()

        # チェックボックス
        self.check_box = {}
        for x in range(5):
            self.check_val[x] = tk.BooleanVar()  # 真偽値で管理
            self.check_val[x].set(False)  # 初期値チェックアウト
            self.check_box[x] = tk.Checkbutton(self.check_frame, text=f'ボタン{x}', variable=self.check_val[x])
            self.check_box[x].pack()

        # メインフレームを最前面に移動
        self.current_frame.tkraise()

    def switch_page_frame(self, current_frame: tk.Frame, switch_page: tk.Frame):
        self.current_frame = current_frame
        switch_page.tkraise()

    def switch_check_frame(self):
        # 切り替え
        self.tmp_frame = self.current_frame
        self.check_frame.tkraise()

    def switch_tmp_frame(self):
        self.tmp_frame.tkraise()
        # ボタンが押されたときにチェックした値を表示する
        for x in range(5):
            if self.check_val[x].get():
                self.check_result.append(f'チェック{x}')
        print('チェック結果', self.check_result)
        # 表示したら次のチェックのために初期化
        self.check_result = []


# メインウィンドウ
root = tk.Tk()
app = App(root)
app.root.mainloop()


0
1
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
0
1