0
1

tkinterを触り始めて躓いたこと

Last updated at Posted at 2023-11-22

はじめに

ファイル共有型のDBの構築とともに万人がDBに情報を登録,照会できるようにとtkinterを使って簡易的なアプリケーションを作り始めました.
作成過程で躓いた内容を掲載します.備忘録です.

動作環境情報

  • Python 3.11.5
  • tkinter 8.6.12

エラー事象

①Entryオブジェクトが取得できない.

入力ボックス(Entryヴィジェット)に入力された値を取得したかった.
-> 変数にtk.Entryを指定したと思っていたら,.placeで位置指定していました.痛恨のミス...!! .packなどでも同様のことが起きます.

【解決策】
Entryオブジェクトの設定と位置設定のコマンドを分けて記述する.

# 修正前
import tkinter as tk

root = tk.Tk()
entry1 = tk.Entry(root,
                  width = 20
                 ).place(x=50, y=50)
# 修正後
import tkinter as tk

root = tk.Tk()
entry1 = tk.Entry(root,
                  width = 20
                 )
entry1.place(x=50, y=50)

これでentry1.get()で入力ボックスの値を取得できるようになりました.


②ボタンのcommandに設定した関数が勝手に実行される.

ボタン押下で関数を実行したかった.
-> 引数付きの関数をボタンのcommandに設定すると,ボタンを設定した関数が実行された際に,ボタン押下で実行する予定の関数も動くマジかあ...

【解決策】
commandに設定した関数に引数がある場合は「lambda:」を入れる.

# 修正前
import tkinter as tk

class Application(tk.Frame):
    def __init__(self, master = None):
# ~~~~~~~~~~ ウィンドウ表示省略 ~~~~~~~~~~ #
        x, y = 1, 2
        tk.Button(
            self.master, 
            text = "qiita",
            command = self.sum(x, y),
            width = 10,
            height = 1
            ).place(x = 135, y = 35)

    def sum(a, b):
        print(a + b)
# 修正後
import tkinter as tk

class Application(tk.Frame):
    def __init__(self, master = None):
# ~~~~~~~~~~ ウィンドウ表示省略 ~~~~~~~~~~ #
        x, y = 1, 2
        tk.Button(
            self.master, 
            text = "qiita",
            command = lambda: self.sum(x, y),
            width = 10,
            height = 1
            ).place(x = 135, y = 35)

    def sum(a, b):
        print(a + b)

③1つのボタンで複数の関数を実行したい.

入力したデータをボタン押下でDBに登録する際,データの格納と確認のためのサブウィンドウを閉じる処理を行いたい.
-> tk.Buttonのcommandオプションだと一つしか設定できない.どうすりゃええんや

【解決策】
commandオプションの代わりにbind()メソッドを使用する.

# 修正前
import tkinter as tk

class Application(tk.Frame):
    def __init__(self, master = None):
# ~~~~~~~~~~ ウィンドウ表示省略 ~~~~~~~~~~ #
        tk.Button(
            self.master, 
            text = "qiita",
            command = self.destroy, # 複数の関数を設定したい
            width = 10,
            height = 1
            ).place(x = 135, y = 35)
# 修正後
import tkinter as tk

class Application(tk.Frame):
    def __init__(self, master = None):
# ~~~~~~~~~~ ウィンドウ表示省略 ~~~~~~~~~~ #
        self.Button = tk.Button(
                            self.master,
                            text = "qiita",
                            width = 10,
                            height = 1
                            )
        self.Button.bind("<ButtonPress>", self.ikhatovo)
        self.Button.bind("<ButtonPress>", self.hellohello, "+")

    def ikhatovo(self, event):
        print("あのイーハトーヴォ")

    def hellohello(self, event):
        print('hello world!')

bindの第三引数に"+"を設定することで前に設定した関数も実行されます.
第三引数を指定しない,あるいは""とすると最後に設定した関数のみ実行されます.


【別手段】メソッド実行用のメソッドを作成する方法↓

import tkinter as tk

class Application(tk.Frame):
    def __init__(self, master = None):
        super().__init__(master)
        self.x, self.y = 1, 2
        tk.Button(
            self.master, 
            text = "qiita",
            command = self.action,
            width = 10,
            height = 1
            ).place(x = 135, y = 35)
    
    def action(self):
        print(self.sum())
        self.close_window()
    
    def sum(self):
        return self.x + self.y
        
    def close_window(self):
        self.master.destroy()

Application(tk.Tk()).mainloop()

おわりに

参考になれば幸いです.
ユーザビリティの高いアプリが作れるよう引き続き学習していきます.

0
1
2

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