質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Tkinter

Tkinterは、GUIツールキットである“Tk”をPythonから利用できるようにした標準ライブラリである。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

658閲覧

tkinter:エントリーボックスを選択して、数字を入力したい。

erisawa

総合スコア12

Tkinter

Tkinterは、GUIツールキットである“Tk”をPythonから利用できるようにした標準ライブラリである。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

1クリップ

投稿2022/02/18 09:36

同じウィンドウに作成した数字キーを用いて、選択したentryボックスに入力したいのですが、現在は1番上のentryボックスにしか数字を入力できていません。

2番目【2】のentryボックスにも入力するにはどうしたら良いでしょうか。
タッチパネルを想定しているのでペンで選択したentryボックスに数字を入力したいです。

import tkinter as tk from tkinter import ttk import cv2 import numpy as np import datetime import os from tkinter import filedialog from tkinter import messagebox class Win5(tk.Frame): def __init__(self,master): super().__init__(master) global exp exp = " " # global variable self.grid(row=0,column=0) self.master.geometry("+10+50")#横×縦 self.master.title("画面") self.create_widgets() self.master.bind_all("<Button-1>", lambda e: self.focus(e)) def focus(self,event): widget = self.master.focus_get() print(widget) def press(self,num): global exp exp=exp + str(num) self.equation1.set(exp) def clear(self): global exp exp = " " self.equation1.set(exp) def create_widgets(self): label_sikiichi=tk.Label(self,text='【1】',font=("",17)) label_sikiichi.grid(row=0,column=0) self.equation1 = tk.StringVar() self.entry_gain = tk.Entry(self,width=20,font=("",17),textvariable = self.equation1) self.entry_gain.grid(row=1,column=0) label_sikiichi2=tk.Label(self,text='【2】',font=("",17)) label_sikiichi2.grid(row=2,column=0) self.entry_rokou = tk.Entry(self,width=20,font=("",17)) self.entry_rokou.grid(row=3,column=0) #適用ボタンの作成 button1 = tk.Button(self,text='OK',width=10,relief="ridge",font=("",18)) button1.grid(row=4,column=1) q = ttk.Button(self,text = '0' , width = 7, command = lambda : self.press('0')) q.grid(row = 5 , column = 2, ipadx = 6 , ipady = 10) w = ttk.Button(self,text = '1' , width = 7, command = lambda : self.press('1')) w.grid(row = 5 , column = 3, ipadx = 6 , ipady = 10) E = ttk.Button(self,text = '2' , width = 7, command = lambda : self.press('2')) E.grid(row = 5 , column = 4, ipadx = 6 , ipady = 10) R = ttk.Button(self,text = '3' , width = 7, command = lambda : self.press('3')) R.grid(row = 6 , column = 2, ipadx = 6 , ipady = 10) T = ttk.Button(self,text = '4' , width = 7, command = lambda : self.press('4')) T.grid(row = 6 , column = 3, ipadx = 6 , ipady = 10) Y = ttk.Button(self,text = '5' , width = 7, command = lambda : self.press('5')) Y.grid(row = 6 , column = 4, ipadx = 6 , ipady = 10) U = ttk.Button(self,text = '6' , width = 7, command = lambda : self.press('6')) U.grid(row = 7 , column = 2, ipadx = 6 , ipady = 10) I = ttk.Button(self,text = '7' , width = 7, command = lambda : self.press('7')) I.grid(row = 7 , column = 3, ipadx = 6 , ipady = 10) O = ttk.Button(self,text = '8' , width = 7, command = lambda : self.press('8')) O.grid(row = 7 , column = 4, ipadx = 6 , ipady = 10) P = ttk.Button(self,text = '9' , width = 7, command = lambda : self.press('9')) P.grid(row = 8 , column = 2, ipadx = 6 , ipady = 10) clear = ttk.Button(self,text = 'Clear' , width = 6, command = self.clear) clear.grid(row = 8 , column = 3, ipadx = 7 , ipady = 10) def main(): global root root = tk.Tk() app = Win5(master=root) app.mainloop() root=app=None if __name__ == "__main__": main()

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

変更箇所が複数あるのでコード全体を載せます。参考にしてみて下さい。

python

1import tkinter as tk 2from tkinter import ttk 3 4class Win5(tk.Frame): 5 def __init__(self,master): 6 super().__init__(master) 7 self.grid(row=0,column=0) 8 self.master.geometry("+10+50")#横×縦 9 self.master.title("画面") 10 self.create_widgets() 11 self.master.bind_all("<Button-1>", lambda e: self.focus(e)) 12 # Entry widgets 13 self.entry_widgets = [w for w in self.winfo_children() if isinstance(w, tk.Entry)] 14 self.entry_hl_colors = [ 15 self.entry_widgets[0].cget('highlightcolor'), 16 self.entry_widgets[0].cget('highlightbackground'), 17 ] 18 self.set_entry_focus(self.entry_widgets[0]) 19 20 def focus(self,event): 21 if isinstance(event.widget, tk.Entry): 22 self.set_entry_focus(event.widget) 23 24 def set_entry_focus(self, w): 25 self.entry_focused = w 26 self.entry_focused.config(highlightcolor='red', highlightbackground='red') 27 for widget in self.entry_widgets: 28 if widget != self.entry_focused: 29 widget.config( 30 highlightcolor=self.entry_hl_colors[0], 31 highlightbackground=self.entry_hl_colors[1]) 32 self.entry_text = tk.StringVar(name=self.entry_focused['textvariable']) 33 34 def press(self,num): 35 exp = self.entry_text.get() + str(num) 36 self.entry_text.set(exp) 37 38 def clear(self): 39 self.entry_focused.delete(0, tk.END) 40 41 def create_widgets(self): 42 label_sikiichi=tk.Label(self,text='【1】',font=("",17)) 43 label_sikiichi.grid(row=0,column=0) 44 self.equation1 = tk.StringVar() 45 self.entry_gain = tk.Entry(self,width=20,font=("",17),textvariable = self.equation1) 46 self.entry_gain.grid(row=1,column=0) 47 label_sikiichi2=tk.Label(self,text='【2】',font=("",17)) 48 label_sikiichi2.grid(row=2,column=0) 49 self.equation2 = tk.StringVar() 50 self.entry_rokou = tk.Entry(self,width=20,font=("",17), textvariable = self.equation2) 51 self.entry_rokou.grid(row=3,column=0) 52 #適用ボタンの作成 53 button1 = tk.Button(self,text='OK',width=10,relief="ridge",font=("",18)) 54 button1.grid(row=4,column=1) 55 56 q = ttk.Button(self,text = '0' , width = 7, command = lambda : self.press('0')) 57 q.grid(row = 5 , column = 2, ipadx = 6 , ipady = 10) 58 59 w = ttk.Button(self,text = '1' , width = 7, command = lambda : self.press('1')) 60 w.grid(row = 5 , column = 3, ipadx = 6 , ipady = 10) 61 62 E = ttk.Button(self,text = '2' , width = 7, command = lambda : self.press('2')) 63 E.grid(row = 5 , column = 4, ipadx = 6 , ipady = 10) 64 65 R = ttk.Button(self,text = '3' , width = 7, command = lambda : self.press('3')) 66 R.grid(row = 6 , column = 2, ipadx = 6 , ipady = 10) 67 68 T = ttk.Button(self,text = '4' , width = 7, command = lambda : self.press('4')) 69 T.grid(row = 6 , column = 3, ipadx = 6 , ipady = 10) 70 71 Y = ttk.Button(self,text = '5' , width = 7, command = lambda : self.press('5')) 72 Y.grid(row = 6 , column = 4, ipadx = 6 , ipady = 10) 73 74 U = ttk.Button(self,text = '6' , width = 7, command = lambda : self.press('6')) 75 U.grid(row = 7 , column = 2, ipadx = 6 , ipady = 10) 76 77 I = ttk.Button(self,text = '7' , width = 7, command = lambda : self.press('7')) 78 I.grid(row = 7 , column = 3, ipadx = 6 , ipady = 10) 79 80 O = ttk.Button(self,text = '8' , width = 7, command = lambda : self.press('8')) 81 O.grid(row = 7 , column = 4, ipadx = 6 , ipady = 10) 82 83 P = ttk.Button(self,text = '9' , width = 7, command = lambda : self.press('9')) 84 P.grid(row = 8 , column = 2, ipadx = 6 , ipady = 10) 85 86 clear = ttk.Button(self,text = 'Clear' , width = 6, command = self.clear) 87 clear.grid(row = 8 , column = 3, ipadx = 7 , ipady = 10) 88 89def main(): 90 global root 91 root = tk.Tk() 92 app = Win5(master=root) 93 app.mainloop() 94 root=app=None 95 96if __name__ == "__main__": 97 main()

投稿2022/02/18 21:34

melian

総合スコア19703

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

erisawa

2022/02/21 02:49

ありがとうございました!無事に解決しました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問