回答編集履歴
1
クリアボタンの追加
answer
CHANGED
@@ -139,4 +139,101 @@
|
|
139
139
|
- **5.** での修正部が複数存在するので、共通部をまとめて関数化する
|
140
140
|
|
141
141
|
などを行おうかと思いましたが、元のコードから構成が大きく変更してしまうので、今回は行っておりません。
|
142
|
-
これらを行うと、もう少しスッキリと書けると思います。
|
142
|
+
これらを行うと、もう少しスッキリと書けると思います。
|
143
|
+
|
144
|
+
---
|
145
|
+
**【補足】**
|
146
|
+
クリアボタン追加してみました
|
147
|
+
|
148
|
+
```Python
|
149
|
+
from tkinter import *
|
150
|
+
import sys
|
151
|
+
|
152
|
+
# spinboxの値が更新された時に呼ばれる関数
|
153
|
+
# Spinbox生成時にこの関数をcommand引数で渡しています。
|
154
|
+
# idxには更新されたSpinboxのIndex値が入ります
|
155
|
+
def update_value(idx):
|
156
|
+
print("Update spinbox {}".format(idx))
|
157
|
+
num_of_purchase = [int(s.get()) for s in spinboxes]
|
158
|
+
sub_total = [n*p for n,p in zip(num_of_purchase, price)]
|
159
|
+
total = sum(sub_total)
|
160
|
+
|
161
|
+
# 表示するデータを作成する(適当に書きました)
|
162
|
+
txt = ["{:5d} x {:2d} = {:5d}".format(p,n,t) for p,n,t in zip(price,num_of_purchase,sub_total)]
|
163
|
+
txt.append("-----------------------------")
|
164
|
+
txt.append("TOTAL : {}".format(total))
|
165
|
+
|
166
|
+
# noteを更新する
|
167
|
+
note.config(text='\n'.join(txt))
|
168
|
+
|
169
|
+
# 全てをクリアする
|
170
|
+
def clear_all():
|
171
|
+
for val in spinbox_values:
|
172
|
+
val.set(0)
|
173
|
+
note.config(text='')
|
174
|
+
|
175
|
+
# Base window
|
176
|
+
window = Tk()
|
177
|
+
|
178
|
+
wc = Canvas(window, width = 2000, height = 2000)
|
179
|
+
wc.create_rectangle(0,0,2000,2000, fill = "paleturquoise")
|
180
|
+
wc.place(x = 0, y = 0)
|
181
|
+
|
182
|
+
# Setting the title
|
183
|
+
window.title("文房具の請求")
|
184
|
+
|
185
|
+
# Coloring canvas
|
186
|
+
for x in range(10):
|
187
|
+
c = Canvas(window, width = 240, height = 150)
|
188
|
+
c.create_rectangle(0,0,240,150, fill = "aliceblue")
|
189
|
+
c.place(x = 30 + int(x/5) * 270, y = 30 + (x%5) * 155)
|
190
|
+
|
191
|
+
# Set Lavels
|
192
|
+
item = ["ノート","鉛筆","シャープペン","消しゴム","色鉛筆","ボールペン","油性ペン","蛍光ペン","はさみ","のり"]
|
193
|
+
for x in range(10):
|
194
|
+
i = Label(text = item[x], bg = "#82EBF7")
|
195
|
+
ko = Label(text = "個", bg = "aliceblue")
|
196
|
+
i.place(x = 30 + int(x/5) * 270, y = 30 + (x%5) * 155)
|
197
|
+
ko.place(x = 115 + int(x/5) * 270, y = 150 + (x%5) * 155)
|
198
|
+
|
199
|
+
# Set Spinboxes
|
200
|
+
spinboxes = []
|
201
|
+
spinbox_values =[]
|
202
|
+
for x in range(10):
|
203
|
+
value = StringVar()
|
204
|
+
s = Spinbox(window, from_=0, to=100, width = 10, textvariable=value, command=lambda idx=x:update_value(idx))
|
205
|
+
s.place(x = 40 + int(x/5) * 270, y = 150 + (x%5) * 155)
|
206
|
+
spinbox_values.append(value)
|
207
|
+
spinboxes.append(s)
|
208
|
+
|
209
|
+
# Set price
|
210
|
+
price = [100,20,150,50,40,120,180,200,350,240]
|
211
|
+
for x in range(10):
|
212
|
+
t = Label(text = "{} 円".format(price[x]), bg = "aliceblue")
|
213
|
+
t.place(x = 30 + int(x/5) * 270, y = 70 + (x%5) * 155)
|
214
|
+
|
215
|
+
# Set name
|
216
|
+
name = ["商品名1","商品名2","商品名3","商品名4","商品名5","商品名6","商品名7","商品名8","商品名9","商品名10"]
|
217
|
+
for x in range(10):
|
218
|
+
n = Label(text = name[x], bg = "aliceblue")
|
219
|
+
n.place(x = 30 + int(x/5) * 270, y = 50 + (x%5) * 155)
|
220
|
+
|
221
|
+
# Set window
|
222
|
+
window.geometry("1000x825+100+20")
|
223
|
+
|
224
|
+
# Set subwindow
|
225
|
+
sb = Canvas(window, width = 400, height = 700)
|
226
|
+
sb.create_rectangle(0,0,400,700, fill = "white")
|
227
|
+
sb.place(x = 570, y = 50)
|
228
|
+
|
229
|
+
text = Label(window, text="カート", bg = "#69D7E3" )
|
230
|
+
text.place(x=570, y=50)
|
231
|
+
|
232
|
+
note = Label(window, text = "", bg = "white" )
|
233
|
+
note.place(x=590, y=80)
|
234
|
+
|
235
|
+
clr_button = Button(window, text = "CLEAR", bg = "white", command=clear_all)
|
236
|
+
clr_button.place(x=580, y= 780)
|
237
|
+
|
238
|
+
window.mainloop()
|
239
|
+
```
|