回答編集履歴

1

クリアボタンの追加

2017/07/04 04:16

投稿

magichan
magichan

スコア15898

test CHANGED
@@ -281,3 +281,197 @@
281
281
  などを行おうかと思いましたが、元のコードから構成が大きく変更してしまうので、今回は行っておりません。
282
282
 
283
283
  これらを行うと、もう少しスッキリと書けると思います。
284
+
285
+
286
+
287
+ ---
288
+
289
+ **【補足】**
290
+
291
+ クリアボタン追加してみました
292
+
293
+
294
+
295
+ ```Python
296
+
297
+ from tkinter import *
298
+
299
+ import sys
300
+
301
+
302
+
303
+ # spinboxの値が更新された時に呼ばれる関数
304
+
305
+ # Spinbox生成時にこの関数をcommand引数で渡しています。
306
+
307
+ # idxには更新されたSpinboxのIndex値が入ります
308
+
309
+ def update_value(idx):
310
+
311
+ print("Update spinbox {}".format(idx))
312
+
313
+ num_of_purchase = [int(s.get()) for s in spinboxes]
314
+
315
+ sub_total = [n*p for n,p in zip(num_of_purchase, price)]
316
+
317
+ total = sum(sub_total)
318
+
319
+
320
+
321
+ # 表示するデータを作成する(適当に書きました)
322
+
323
+ txt = ["{:5d} x {:2d} = {:5d}".format(p,n,t) for p,n,t in zip(price,num_of_purchase,sub_total)]
324
+
325
+ txt.append("-----------------------------")
326
+
327
+ txt.append("TOTAL : {}".format(total))
328
+
329
+
330
+
331
+ # noteを更新する
332
+
333
+ note.config(text='\n'.join(txt))
334
+
335
+
336
+
337
+ # 全てをクリアする
338
+
339
+ def clear_all():
340
+
341
+ for val in spinbox_values:
342
+
343
+ val.set(0)
344
+
345
+ note.config(text='')
346
+
347
+
348
+
349
+ # Base window
350
+
351
+ window = Tk()
352
+
353
+
354
+
355
+ wc = Canvas(window, width = 2000, height = 2000)
356
+
357
+ wc.create_rectangle(0,0,2000,2000, fill = "paleturquoise")
358
+
359
+ wc.place(x = 0, y = 0)
360
+
361
+
362
+
363
+ # Setting the title
364
+
365
+ window.title("文房具の請求")
366
+
367
+
368
+
369
+ # Coloring canvas
370
+
371
+ for x in range(10):
372
+
373
+ c = Canvas(window, width = 240, height = 150)
374
+
375
+ c.create_rectangle(0,0,240,150, fill = "aliceblue")
376
+
377
+ c.place(x = 30 + int(x/5) * 270, y = 30 + (x%5) * 155)
378
+
379
+
380
+
381
+ # Set Lavels
382
+
383
+ item = ["ノート","鉛筆","シャープペン","消しゴム","色鉛筆","ボールペン","油性ペン","蛍光ペン","はさみ","のり"]
384
+
385
+ for x in range(10):
386
+
387
+ i = Label(text = item[x], bg = "#82EBF7")
388
+
389
+ ko = Label(text = "個", bg = "aliceblue")
390
+
391
+ i.place(x = 30 + int(x/5) * 270, y = 30 + (x%5) * 155)
392
+
393
+ ko.place(x = 115 + int(x/5) * 270, y = 150 + (x%5) * 155)
394
+
395
+
396
+
397
+ # Set Spinboxes
398
+
399
+ spinboxes = []
400
+
401
+ spinbox_values =[]
402
+
403
+ for x in range(10):
404
+
405
+ value = StringVar()
406
+
407
+ s = Spinbox(window, from_=0, to=100, width = 10, textvariable=value, command=lambda idx=x:update_value(idx))
408
+
409
+ s.place(x = 40 + int(x/5) * 270, y = 150 + (x%5) * 155)
410
+
411
+ spinbox_values.append(value)
412
+
413
+ spinboxes.append(s)
414
+
415
+
416
+
417
+ # Set price
418
+
419
+ price = [100,20,150,50,40,120,180,200,350,240]
420
+
421
+ for x in range(10):
422
+
423
+ t = Label(text = "{} 円".format(price[x]), bg = "aliceblue")
424
+
425
+ t.place(x = 30 + int(x/5) * 270, y = 70 + (x%5) * 155)
426
+
427
+
428
+
429
+ # Set name
430
+
431
+ name = ["商品名1","商品名2","商品名3","商品名4","商品名5","商品名6","商品名7","商品名8","商品名9","商品名10"]
432
+
433
+ for x in range(10):
434
+
435
+ n = Label(text = name[x], bg = "aliceblue")
436
+
437
+ n.place(x = 30 + int(x/5) * 270, y = 50 + (x%5) * 155)
438
+
439
+
440
+
441
+ # Set window
442
+
443
+ window.geometry("1000x825+100+20")
444
+
445
+
446
+
447
+ # Set subwindow
448
+
449
+ sb = Canvas(window, width = 400, height = 700)
450
+
451
+ sb.create_rectangle(0,0,400,700, fill = "white")
452
+
453
+ sb.place(x = 570, y = 50)
454
+
455
+
456
+
457
+ text = Label(window, text="カート", bg = "#69D7E3" )
458
+
459
+ text.place(x=570, y=50)
460
+
461
+
462
+
463
+ note = Label(window, text = "", bg = "white" )
464
+
465
+ note.place(x=590, y=80)
466
+
467
+
468
+
469
+ clr_button = Button(window, text = "CLEAR", bg = "white", command=clear_all)
470
+
471
+ clr_button.place(x=580, y= 780)
472
+
473
+
474
+
475
+ window.mainloop()
476
+
477
+ ```