前提
tkinterで複数の入力部があり、入力部に整数値を入力した後にボタンを押下する機能を持つテキストボックスを生成する。
実現したいこと
入力部に記入した複数の整数値を、ボタンを押下したタイミングでそれぞれ別の変数に格納したい。
発生している問題・エラーメッセージ
Exception in Tkinter callback Traceback (most recent call last): File "c:\Users\(ユーザー名)\Miniconda3\envs\(仮想環境名)\lib\tkinter\__init__.py", line 1892, in __call__ return self.func(*args) File "C:\Users\(ユーザー名)\AppData\Local\Temp\ipykernel_19848\3324598238.py", line 30, in val x1 = textBox1.get() AttributeError: 'NoneType' object has no attribute 'get'
該当のソースコード
python3.9.13
1import tkinter as tk 2 3 4# Tkインスタンスの作成 5root = tk.Tk() 6 7# ウィンドウのサイズを設定 8root.geometry('500x330') 9# 画面タイトル 10root.title('変数を入力してください') 11 12# ラベル 13label1 = tk.Label(text='text1').place(x=30, y=30) 14label2 = tk.Label(text='text2').place(x=30, y=70) 15label3 = tk.Label(text='text3').place(x=30, y=110) 16label4 = tk.Label(text='text4').place(x=30, y=150) 17label5 = tk.Label(text='text5').place(x=30, y=190) 18label6 = tk.Label(text='text6').place(x=30, y=230) 19 20# テキストボックス 21textBox1 = tk.Entry(width=30).place(x=250, y=30) 22textBox2 = tk.Entry(width=30).place(x=250, y=70) 23textBox3 = tk.Entry(width=30).place(x=250, y=110) 24textBox4 = tk.Entry(width=30).place(x=250, y=150) 25textBox5 = tk.Entry(width=30).place(x=250, y=190) 26textBox6 = tk.Entry(width=30).place(x=250, y=230) 27 28# テキストボックスの値を取得 29def val(): 30 x1 = textBox1.get() 31 x2 = textBox2.get() 32 x3 = textBox3.get() 33 x4 = textBox4.get() 34 x5 = textBox5.get() 35 x6 = textBox6.get() 36 return x1, x2, x3, x4, x5, x6 37 38# ボタンの作成と配置 39button = tk.Button(root, 40 text = '送信', 41 # クリック時にval()関数を呼ぶ 42 command = val 43 ).place(x=30, y=270) 44 45root.mainloop()
試したこと
上記のスクリプトを試した後に、下記リンクの回答を参考にすることで、1つの入力値を1つの変数に格納することはできましたが、これを複数にする方法については、調べても情報にたどり着きませんでした。
https://teratail.com/questions/238682
ご教示いただけますと大変助かります。
補足情報(FW/ツールのバージョンなど)
windows 10 enterprise 64bit
Miniconda3 4.11.0
Python 3.9.13

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2023/01/26 11:03
2023/01/26 23:50