質問編集履歴

8

修正

2020/03/18 07:22

投稿

person
person

スコア223

test CHANGED
File without changes
test CHANGED
@@ -214,98 +214,98 @@
214
214
 
215
215
  on_closing()
216
216
 
217
+
218
+
219
+ def push_clear():
220
+
221
+ e_id.delete(0, tk.END)
222
+
223
+
224
+
225
+ def push_enter():
226
+
227
+ input_widget.insert(tk.END, e_id.get())
228
+
229
+ on_closing()
230
+
231
+
232
+
233
+ def connected(tag):
234
+
235
+ idm = binascii.hexlify(tag.idm) # <class 'byte'>
236
+
237
+ idm = idm.decode() # <class 'str'>
238
+
239
+ e_id.insert(tk.END, idm)
240
+
241
+ return True # これがないとICを1回かざしたときに複数回認識してしまう(whileループ時)
242
+
243
+
244
+
245
+ def ic_read():
246
+
247
+ global clf
248
+
249
+ clf = nfc.ContactlessFrontend("usb") # 接続
250
+
251
+ try:
252
+
253
+ clf.connect( rdwr={"on-connect": connected} ) # 認識
254
+
255
+ finally:
256
+
257
+ clf.close() # 切断
258
+
259
+
260
+
261
+ def on_closing():
262
+
263
+ global clf
264
+
265
+ clf.close() # これがないと応答なしになってしまう
266
+
267
+ thread_nfc.join()
268
+
217
269
  sub_win.destroy()
218
270
 
219
271
 
220
272
 
221
- def push_clear():
222
-
223
- e_id.delete(0, tk.END)
224
-
225
-
226
-
227
- def push_enter():
228
-
229
- input_widget.insert(tk.END, e_id.get())
230
-
231
- on_closing()
232
-
233
- sub_win.destroy()
273
+ sub_win = tk.Toplevel()
234
274
 
235
275
 
236
276
 
237
- def connected(tag):
238
-
239
- idm = binascii.hexlify(tag.idm) # <class 'byte'>
240
-
241
- idm = idm.decode() # <class 'str'>
242
-
243
- e_id.insert(tk.END, idm)
244
-
245
- return True # これがないとICを1回かざしたときに複数回認識してしまう(whileループ時)
246
-
247
-
248
-
249
- def ic_read():
250
-
251
- global clf
252
-
253
- clf = nfc.ContactlessFrontend("usb") # 接続
277
+ thread_nfc = threading.Thread(target=ic_read)
254
-
255
- try:
278
+
256
-
257
- clf.connect( rdwr={"on-connect": connected} ) # 認識
258
-
259
- finally:
260
-
261
- clf.close() # 切断
262
-
263
-
264
-
265
- def on_closing():
266
-
267
- global clf
268
-
269
- clf.close() # これがないと応答なしになってしまう
270
-
271
- thread_nfc.join()
279
+ thread_nfc.start()
272
-
273
-
274
-
280
+
281
+
282
+
275
- sub_win = tk.Toplevel()
283
+ e_id = tk.Entry(sub_win)
284
+
285
+ e_id.insert(tk.END, input_widget.get())
286
+
287
+ e_id.grid()
288
+
289
+
290
+
291
+ b_cls = tk.Button(sub_win, text="CLOSE", command=push_close)
292
+
293
+ b_cls.grid()
294
+
295
+
296
+
297
+ b_clr = tk.Button(sub_win, text="CLEAR", command=push_clear)
298
+
299
+ b_clr.grid()
300
+
301
+
302
+
303
+ b_etr = tk.Button(sub_win, text="ENTER", command=push_enter)
304
+
305
+ b_etr.grid()
276
306
 
277
307
 
278
308
 
279
- thread_nfc = threading.Thread(target=ic_read)
280
-
281
- thread_nfc.start()
282
-
283
-
284
-
285
- e_id = tk.Entry(sub_win)
286
-
287
- e_id.insert(tk.END, input_widget.get())
288
-
289
- e_id.grid()
290
-
291
-
292
-
293
- b_cls = tk.Button(sub_win, text="CLOSE", command=push_close)
309
+ sub_win.protocol("WM_DELETE_WINDOW", on_closing)
294
-
295
- b_cls.grid()
296
-
297
-
298
-
299
- b_clr = tk.Button(sub_win, text="CLEAR", command=push_clear)
300
-
301
- b_clr.grid()
302
-
303
-
304
-
305
- b_etr = tk.Button(sub_win, text="ENTER", command=push_enter)
306
-
307
- b_etr.grid()
308
-
309
-
310
310
 
311
311
  ```

7

追加

2020/03/18 07:22

投稿

person
person

スコア223

test CHANGED
File without changes
test CHANGED
@@ -242,7 +242,7 @@
242
242
 
243
243
  e_id.insert(tk.END, idm)
244
244
 
245
- return True # これがないとICを1回かざしたときに複数回認識してしまう
245
+ return True # これがないとICを1回かざしたときに複数回認識してしまう(whileループ時)
246
246
 
247
247
 
248
248
 
@@ -266,7 +266,7 @@
266
266
 
267
267
  global clf
268
268
 
269
- clf.close()
269
+ clf.close() # これがないと応答なしになってしまう
270
270
 
271
271
  thread_nfc.join()
272
272
 

6

修正

2020/03/17 02:15

投稿

person
person

スコア223

test CHANGED
File without changes
test CHANGED
@@ -124,7 +124,7 @@
124
124
 
125
125
 
126
126
 
127
- **追記(ソースの間違い)**
127
+ **修正(ソースの間違い)**
128
128
 
129
129
  ```Python
130
130
 

5

追加

2020/03/17 02:14

投稿

person
person

スコア223

test CHANGED
File without changes
test CHANGED
@@ -121,3 +121,191 @@
121
121
  win.mainloop()
122
122
 
123
123
  ```
124
+
125
+
126
+
127
+ **追記(ソースの間違い)**
128
+
129
+ ```Python
130
+
131
+ # main.py
132
+
133
+
134
+
135
+ import tkinter as tk
136
+
137
+
138
+
139
+ import input_ic
140
+
141
+
142
+
143
+
144
+
145
+ def push_enter():
146
+
147
+ print(e_id.get())
148
+
149
+ e_id.delete(0, tk.END)
150
+
151
+
152
+
153
+
154
+
155
+ def push_entry(e, e_id):
156
+
157
+ input_ic.input_ic(e, e_id)
158
+
159
+
160
+
161
+
162
+
163
+ if __name__ == "__main__":
164
+
165
+ win = tk.Tk()
166
+
167
+
168
+
169
+ e_id = tk.Entry(win)
170
+
171
+ e_id.grid()
172
+
173
+
174
+
175
+ b_etr = tk.Button(win, text="ENTER", command=push_enter)
176
+
177
+ b_etr.grid()
178
+
179
+
180
+
181
+ e_id.bind("<Button-1>", lambda e:push_entry(e, e_id))
182
+
183
+
184
+
185
+ win.mainloop()
186
+
187
+ ```
188
+
189
+
190
+
191
+ ```Python
192
+
193
+ # input_ic.py
194
+
195
+
196
+
197
+ import nfc
198
+
199
+ import tkinter as tk
200
+
201
+ import binascii
202
+
203
+ import threading
204
+
205
+
206
+
207
+
208
+
209
+ def input_ic(e, input_widget):
210
+
211
+
212
+
213
+ def push_close():
214
+
215
+ on_closing()
216
+
217
+ sub_win.destroy()
218
+
219
+
220
+
221
+ def push_clear():
222
+
223
+ e_id.delete(0, tk.END)
224
+
225
+
226
+
227
+ def push_enter():
228
+
229
+ input_widget.insert(tk.END, e_id.get())
230
+
231
+ on_closing()
232
+
233
+ sub_win.destroy()
234
+
235
+
236
+
237
+ def connected(tag):
238
+
239
+ idm = binascii.hexlify(tag.idm) # <class 'byte'>
240
+
241
+ idm = idm.decode() # <class 'str'>
242
+
243
+ e_id.insert(tk.END, idm)
244
+
245
+ return True # これがないとICを1回かざしたときに複数回認識してしまう
246
+
247
+
248
+
249
+ def ic_read():
250
+
251
+ global clf
252
+
253
+ clf = nfc.ContactlessFrontend("usb") # 接続
254
+
255
+ try:
256
+
257
+ clf.connect( rdwr={"on-connect": connected} ) # 認識
258
+
259
+ finally:
260
+
261
+ clf.close() # 切断
262
+
263
+
264
+
265
+ def on_closing():
266
+
267
+ global clf
268
+
269
+ clf.close()
270
+
271
+ thread_nfc.join()
272
+
273
+
274
+
275
+ sub_win = tk.Toplevel()
276
+
277
+
278
+
279
+ thread_nfc = threading.Thread(target=ic_read)
280
+
281
+ thread_nfc.start()
282
+
283
+
284
+
285
+ e_id = tk.Entry(sub_win)
286
+
287
+ e_id.insert(tk.END, input_widget.get())
288
+
289
+ e_id.grid()
290
+
291
+
292
+
293
+ b_cls = tk.Button(sub_win, text="CLOSE", command=push_close)
294
+
295
+ b_cls.grid()
296
+
297
+
298
+
299
+ b_clr = tk.Button(sub_win, text="CLEAR", command=push_clear)
300
+
301
+ b_clr.grid()
302
+
303
+
304
+
305
+ b_etr = tk.Button(sub_win, text="ENTER", command=push_enter)
306
+
307
+ b_etr.grid()
308
+
309
+
310
+
311
+ ```

4

追加

2020/03/17 02:12

投稿

person
person

スコア223

test CHANGED
File without changes
test CHANGED
@@ -16,7 +16,7 @@
16
16
 
17
17
  上手く動作しないので、どこをどう直せばよいか教えてください。
18
18
 
19
- (ICを読み込ませずにウィンドウを閉じるとができない
19
+ (ICを読み込ませずにウィンドウを閉じようとすると、閉じるまでに時間かかりそれまはフリーズ状態
20
20
 
21
21
 
22
22
 
@@ -48,6 +48,8 @@
48
48
 
49
49
  def ic_read():
50
50
 
51
+ global clf # on_close() でも扱うことがあるため
52
+
51
53
  clf = nfc.ContactlessFrontend("usb") # 接続
52
54
 
53
55
  try:
@@ -73,6 +75,10 @@
73
75
 
74
76
 
75
77
  def on_closing():
78
+
79
+ global clf
80
+
81
+ clf.close() # ICを読み込まずにウィンドウを閉じようとするときに、これがないとフリーズではなく応答なしになってしまう
76
82
 
77
83
  thread_nfc.join()
78
84
 

3

修正

2020/02/28 01:27

投稿

person
person

スコア223

test CHANGED
File without changes
test CHANGED
@@ -16,7 +16,7 @@
16
16
 
17
17
  上手く動作しないので、どこをどう直せばよいか教えてください。
18
18
 
19
- (読み取りはするが、ウィンドウを閉じることができない)
19
+ ICを読み込ませずにウィンドウを閉じることができない)
20
20
 
21
21
 
22
22
 
@@ -76,6 +76,8 @@
76
76
 
77
77
  thread_nfc.join()
78
78
 
79
+ win.destroy()
80
+
79
81
 
80
82
 
81
83
  if __name__ == "__main__":

2

修正

2020/02/28 00:57

投稿

person
person

スコア223

test CHANGED
File without changes
test CHANGED
@@ -16,7 +16,7 @@
16
16
 
17
17
  上手く動作しないので、どこをどう直せばよいか教えてください。
18
18
 
19
- (読み取りはするが、読み取った後にウィンドウがてしまう
19
+ (読み取りはするが、ウィンドウを閉じることできない)
20
20
 
21
21
 
22
22
 
@@ -27,6 +27,8 @@
27
27
  import tkinter as tk
28
28
 
29
29
  import binascii
30
+
31
+ import threading
30
32
 
31
33
 
32
34
 
@@ -66,11 +68,23 @@
66
68
 
67
69
  e_id.insert(tk.END, idm)
68
70
 
69
- return True # 複数回かざす場合にこれがないと瞬間的に連続認識してしまう(?)
71
+ return True
72
+
73
+
74
+
75
+ def on_closing():
76
+
77
+ thread_nfc.join()
70
78
 
71
79
 
72
80
 
73
81
  if __name__ == "__main__":
82
+
83
+ thread_nfc = threading.Thread(target=ic_read)
84
+
85
+ thread_nfc.start()
86
+
87
+
74
88
 
75
89
  win = tk.Tk()
76
90
 
@@ -94,9 +108,7 @@
94
108
 
95
109
 
96
110
 
97
- ic_read() #意図した動作をしない
111
+ win.protocol("WM_DELETE_WINDOW", on_closing)
98
-
99
-
100
112
 
101
113
  win.mainloop()
102
114
 

1

追加

2020/02/28 00:54

投稿

person
person

スコア223

test CHANGED
File without changes
test CHANGED
@@ -2,13 +2,21 @@
2
2
 
3
3
 
4
4
 
5
- ICは1回読めればいいです
5
+ ICリーダRC-S380を使用
6
+
7
+
8
+
9
+ ICは1回読めればいいです。もう1回かざしなおしても読み取らなくていいです。
6
10
 
7
11
 
8
12
 
9
13
  自分なりに考えて下のようになりました。
10
14
 
15
+
16
+
11
17
  上手く動作しないので、どこをどう直せばよいか教えてください。
18
+
19
+ (読み取りはするが、読み取った後にウィンドウが開いてしまう)
12
20
 
13
21
 
14
22