質問編集履歴
1
test
CHANGED
File without changes
|
test
CHANGED
@@ -10,538 +10,6 @@
|
|
10
10
|
|
11
11
|
# -*- coding: utf-8 -*-
|
12
12
|
|
13
|
-
from tkinter import *
|
14
|
-
|
15
|
-
import tkinter as tk
|
16
|
-
|
17
|
-
# python2の場合は、import Tkinter as tk
|
18
|
-
|
19
|
-
import tkinter.ttk as ttk
|
20
|
-
|
21
|
-
# python2の場合は、import ttk
|
22
|
-
|
23
|
-
import sqlite3
|
24
|
-
|
25
|
-
import sys
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
# ログイン画面関数
|
30
|
-
|
31
|
-
def login_gui():
|
32
|
-
|
33
|
-
#ログインの認証
|
34
|
-
|
35
|
-
def foo():
|
36
|
-
|
37
|
-
#正規のユーザーの時create_gui(メイン画面)へ
|
38
|
-
|
39
|
-
if t.get() == "hello":
|
40
|
-
|
41
|
-
root.destroy()
|
42
|
-
|
43
|
-
create_gui()
|
44
|
-
|
45
|
-
#ユーザ名が違うときpass
|
46
|
-
|
47
|
-
else:
|
48
|
-
|
49
|
-
pass
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
# root定義
|
54
|
-
|
55
|
-
root = tk.Tk()
|
56
|
-
|
57
|
-
root.title('login')
|
58
|
-
|
59
|
-
frame1 = tk.Frame(root)
|
60
|
-
|
61
|
-
label1 = tk.Label(frame1, text='Your name:')
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
#入力された文字の取得
|
66
|
-
|
67
|
-
t = tk.StringVar()
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
#メニュー定義
|
72
|
-
|
73
|
-
entry1 = tk.Entry(frame1, textvariable=t)
|
74
|
-
|
75
|
-
button1 = tk.Button(frame1, text='OK', command=foo)
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
frame1.grid(row=0,column=0,sticky=(N,E,S,W))
|
80
|
-
|
81
|
-
label1.grid(row=1,column=1,sticky=E)
|
82
|
-
|
83
|
-
entry1.grid(row=1,column=2,sticky=W)
|
84
|
-
|
85
|
-
button1.grid(row=2,column=2,sticky=W)
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
for child in frame1.winfo_children():
|
90
|
-
|
91
|
-
child.grid_configure(padx=5, pady=5)
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
root.mainloop()
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
# 登録画面のGUI
|
106
|
-
|
107
|
-
def create_gui():
|
108
|
-
|
109
|
-
# ----------------------------------------
|
110
|
-
|
111
|
-
# コールバック関数群
|
112
|
-
|
113
|
-
# ----------------------------------------
|
114
|
-
|
115
|
-
# 表示ボタンが押下されたときのコールバック関数
|
116
|
-
|
117
|
-
def select_button():
|
118
|
-
|
119
|
-
root.destroy()
|
120
|
-
|
121
|
-
select_gui()
|
122
|
-
|
123
|
-
# ----------------------------------------
|
124
|
-
|
125
|
-
# 終了ボタンが押下されたときのコールバック関数
|
126
|
-
|
127
|
-
def quit_button():
|
128
|
-
|
129
|
-
root.destroy()
|
130
|
-
|
131
|
-
# ----------------------------------------
|
132
|
-
|
133
|
-
# 登録ボタンがクリックされた時にデータをDBに登録するコールバック関数
|
134
|
-
|
135
|
-
def create_sql(item_name):
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
# データベースに接続
|
140
|
-
|
141
|
-
c = sqlite3.connect("database.db")
|
142
|
-
|
143
|
-
# item_nameをWHERE句に渡してitem_codeを取得する
|
144
|
-
|
145
|
-
item_code = c.execute("""
|
146
|
-
|
147
|
-
SELECT item_code FROM item
|
148
|
-
|
149
|
-
WHERE item_name = '{}'
|
150
|
-
|
151
|
-
""".format(item_name))
|
152
|
-
|
153
|
-
item_code = item_code.fetchone()[0]
|
154
|
-
|
155
|
-
# 日付の読み取り
|
156
|
-
|
157
|
-
acc_data = entry1.get().replace("/","-")
|
158
|
-
|
159
|
-
# 金額の読み取り
|
160
|
-
|
161
|
-
amount = entry3.get()
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
# SQLを発行してDBへ登録
|
166
|
-
|
167
|
-
# python2の場合は、ユニコード文字列でsqlite3に渡す
|
168
|
-
|
169
|
-
# また、コミットする場合は、commitメソッドを用いる
|
170
|
-
|
171
|
-
try:
|
172
|
-
|
173
|
-
c.execute("""
|
174
|
-
|
175
|
-
INSERT INTO acc_data(acc_date,item_code,amount)
|
176
|
-
|
177
|
-
VALUES('{}',{},{});
|
178
|
-
|
179
|
-
""".format(acc_data,item_code,amount))
|
180
|
-
|
181
|
-
c.execute("COMMIT;")
|
182
|
-
|
183
|
-
print("1件登録しました")
|
184
|
-
|
185
|
-
# ドメインエラーなどにより登録できなかった場合のエラー処理
|
186
|
-
|
187
|
-
except:
|
188
|
-
|
189
|
-
print("エラーにより登録できませんでした")
|
190
|
-
|
191
|
-
# ----------------------------------------
|
192
|
-
|
193
|
-
# 内訳テーブル(item)にあるitem_nameのタプルを作成する
|
194
|
-
|
195
|
-
def createitemname():
|
196
|
-
|
197
|
-
# データベースの接続
|
198
|
-
|
199
|
-
c = sqlite3.connect("database.db")
|
200
|
-
|
201
|
-
# 空の「リスト型」を定義
|
202
|
-
|
203
|
-
li = []
|
204
|
-
|
205
|
-
# SELECT文を発行し、item_nameを取得し、for文で回す
|
206
|
-
|
207
|
-
for r in c.execute("SELECT item_name FROM item"):
|
208
|
-
|
209
|
-
# item_nameをリストに追加する
|
210
|
-
|
211
|
-
li.append(r)
|
212
|
-
|
213
|
-
# リスト型のliをタプル型に変換して、ファンクションに戻す
|
214
|
-
|
215
|
-
return tuple(li)
|
216
|
-
|
217
|
-
# ----------------------------------------
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
# 空のデータベースを作成して接続する
|
222
|
-
|
223
|
-
dbname = "database.db"
|
224
|
-
|
225
|
-
c = sqlite3.connect(dbname)
|
226
|
-
|
227
|
-
c.execute("PRAGMA foreign_keys = 1")
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
# 既にデータベースが登録されている場合は、ddlの発行でエラーが出るのでexceptブロックで回避する
|
232
|
-
|
233
|
-
try:
|
234
|
-
|
235
|
-
# itemテーブルの定義
|
236
|
-
|
237
|
-
ddl = """
|
238
|
-
|
239
|
-
CREATE TABLE item
|
240
|
-
|
241
|
-
(
|
242
|
-
|
243
|
-
item_code INTEGER PRIMARY KEY AUTOINCREMENT,
|
244
|
-
|
245
|
-
item_name TEXT NOT NULL UNIQUE
|
246
|
-
|
247
|
-
)
|
248
|
-
|
249
|
-
"""
|
250
|
-
|
251
|
-
# SQLの発行
|
252
|
-
|
253
|
-
c.execute(ddl)
|
254
|
-
|
255
|
-
# acc_dataテーブルの定義
|
256
|
-
|
257
|
-
ddl = """
|
258
|
-
|
259
|
-
CREATE TABLE acc_data
|
260
|
-
|
261
|
-
(
|
262
|
-
|
263
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
264
|
-
|
265
|
-
acc_date DATE NOT NULL,
|
266
|
-
|
267
|
-
item_code INTEGER NOT NULL,
|
268
|
-
|
269
|
-
amount INTEGER,
|
270
|
-
|
271
|
-
FOREIGN KEY(item_code) REFERENCES item(item_code)
|
272
|
-
|
273
|
-
)
|
274
|
-
|
275
|
-
"""
|
276
|
-
|
277
|
-
# itemテーブルへリファレンスデータの登録
|
278
|
-
|
279
|
-
# 初回起動時に書き換えることで変更可能
|
280
|
-
|
281
|
-
c.execute(ddl)
|
282
|
-
|
283
|
-
c.execute("INSERT INTO item VALUES(1,'食費')")
|
284
|
-
|
285
|
-
c.execute("INSERT INTO item VALUES(2,'住宅費')")
|
286
|
-
|
287
|
-
c.execute("INSERT INTO item VALUES(3,'光熱費')")
|
288
|
-
|
289
|
-
c.execute("COMMIT")
|
290
|
-
|
291
|
-
except:
|
292
|
-
|
293
|
-
pass
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
# rootフレームの設定
|
298
|
-
|
299
|
-
root = tk.Tk()
|
300
|
-
|
301
|
-
root.title("家計簿アプリ")
|
302
|
-
|
303
|
-
root.geometry("300x280")
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
# メニューの設定
|
308
|
-
|
309
|
-
frame = tk.Frame(root,bd=2,relief="ridge")
|
310
|
-
|
311
|
-
frame.pack(fill="x")
|
312
|
-
|
313
|
-
button1 = tk.Button(frame,text="入力")
|
314
|
-
|
315
|
-
button1.pack(side="left")
|
316
|
-
|
317
|
-
button2 = tk.Button(frame,text="表示",command=select_button)
|
318
|
-
|
319
|
-
button2.pack(side="left")
|
320
|
-
|
321
|
-
button3 = tk.Button(frame,text="終了",command=quit_button)
|
322
|
-
|
323
|
-
button3.pack(side="right")
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
# 入力画面ラベルの設定
|
328
|
-
|
329
|
-
label1 = tk.Label(root,text="【入力画面】",font=("",16),height=2)
|
330
|
-
|
331
|
-
label1.pack(fill="x")
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
# 日付のラベルとエントリーの設定
|
336
|
-
|
337
|
-
frame1 = tk.Frame(root,pady=10)
|
338
|
-
|
339
|
-
frame1.pack()
|
340
|
-
|
341
|
-
label2 = tk.Label(frame1,font=("",14),text="日付")
|
342
|
-
|
343
|
-
label2.pack(side="left")
|
344
|
-
|
345
|
-
entry1 = tk.Entry(frame1,font=("",14),justify="center",width=15)
|
346
|
-
|
347
|
-
entry1.pack(side="left")
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
# 内訳のラベルとエントリーの設定
|
352
|
-
|
353
|
-
frame2 = tk.Frame(root,pady=10)
|
354
|
-
|
355
|
-
frame2.pack()
|
356
|
-
|
357
|
-
label3 = tk.Label(frame2,font=("",14),text="内訳")
|
358
|
-
|
359
|
-
label3.pack(side="left")
|
360
|
-
|
361
|
-
# 内訳コンボボックスの作成
|
362
|
-
|
363
|
-
combo = ttk.Combobox(frame2, state='readonly',font=("",14),width=13)
|
364
|
-
|
365
|
-
combo["values"] = createitemname()
|
366
|
-
|
367
|
-
combo.current(0)
|
368
|
-
|
369
|
-
combo.pack()
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
# 金額のラベルとエントリーの設定
|
374
|
-
|
375
|
-
frame3 = tk.Frame(root,pady=10)
|
376
|
-
|
377
|
-
frame3.pack()
|
378
|
-
|
379
|
-
label4 = tk.Label(frame3,font=("",14),text="金額")
|
380
|
-
|
381
|
-
label4.pack(side="left")
|
382
|
-
|
383
|
-
entry3 = tk.Entry(frame3,font=("",14),justify="center",width=15)
|
384
|
-
|
385
|
-
entry3.pack(side="left")
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
# 登録ボタンの設定
|
390
|
-
|
391
|
-
button4 = tk.Button(root,text="登録",
|
392
|
-
|
393
|
-
font=("",16),
|
394
|
-
|
395
|
-
width=10,bg="gray",
|
396
|
-
|
397
|
-
command=lambda:create_sql(combo.get()))
|
398
|
-
|
399
|
-
button4.pack()
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
root.mainloop()
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
login_gui()
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
# 表示画面のGUI
|
418
|
-
|
419
|
-
def select_gui():
|
420
|
-
|
421
|
-
# ----------------------------------------
|
422
|
-
|
423
|
-
# コールバック関数群
|
424
|
-
|
425
|
-
# ----------------------------------------
|
426
|
-
|
427
|
-
# 登録ボタンが押下されたときのコールバック関数
|
428
|
-
|
429
|
-
def create_button():
|
430
|
-
|
431
|
-
root.destroy()
|
432
|
-
|
433
|
-
create_gui()
|
434
|
-
|
435
|
-
# ----------------------------------------
|
436
|
-
|
437
|
-
# 終了ボタンが押下されたときのコールバック関数
|
438
|
-
|
439
|
-
def quit_button():
|
440
|
-
|
441
|
-
root.destroy()
|
442
|
-
|
443
|
-
# ----------------------------------------
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
# rootフレームの設定
|
448
|
-
|
449
|
-
root = tk.Tk()
|
450
|
-
|
451
|
-
root.title("家計簿アプリ")
|
452
|
-
|
453
|
-
root.geometry("700x700")
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
# メニューの設定
|
458
|
-
|
459
|
-
frame = tk.Frame(root,bd=2,relief="ridge")
|
460
|
-
|
461
|
-
frame.pack(fill="x")
|
462
|
-
|
463
|
-
button1 = tk.Button(frame,text="入力",command=create_button)
|
464
|
-
|
465
|
-
button1.pack(side="left")
|
466
|
-
|
467
|
-
button2 = tk.Button(frame,text="表示")
|
468
|
-
|
469
|
-
button2.pack(side="left")
|
470
|
-
|
471
|
-
button3 = tk.Button(frame,text="終了",command=quit_button)
|
472
|
-
|
473
|
-
button3.pack(side="right")
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
# 入力画面ラベルの設定
|
480
|
-
|
481
|
-
label1 = tk.Label(root,text="【表示画面】",font=("",16),height=2)
|
482
|
-
|
483
|
-
label1.pack(fill="x")
|
484
|
-
|
485
|
-
chk=tk.BooleanVar()
|
486
|
-
|
487
|
-
chk.set(True)
|
488
|
-
|
489
|
-
chko=tk.BooleanVar()
|
490
|
-
|
491
|
-
chko.set(False)
|
492
|
-
|
493
|
-
chk1=tk.Checkbutton(root,text='収入を表示',variable=chk)
|
494
|
-
|
495
|
-
chk1.pack()
|
496
|
-
|
497
|
-
chk2=tk.Checkbutton(root,text='支出を表示',variable=chko)
|
498
|
-
|
499
|
-
chk2.pack()
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
# 期間選択のラベルエントリーの設定
|
508
|
-
|
509
|
-
frame1 = tk.Frame(root,pady=15)
|
510
|
-
|
511
|
-
frame1.pack()
|
512
|
-
|
513
|
-
label2 = tk.Label(frame1,font=("",14),text="期間 ")
|
514
|
-
|
515
|
-
label2.pack(side="left")
|
516
|
-
|
517
|
-
entry1 = tk.Entry(frame1,font=("",14),justify="center",width=12)
|
518
|
-
|
519
|
-
entry1.pack(side="left")
|
520
|
-
|
521
|
-
label3 = tk.Label(frame1,font=("",14),text=" ~ ")
|
522
|
-
|
523
|
-
label3.pack(side="left")
|
524
|
-
|
525
|
-
entry2 = tk.Entry(frame1,font=("",14),justify="center",width=12)
|
526
|
-
|
527
|
-
entry2.pack(side="left")
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
# メインループ
|
532
|
-
|
533
|
-
root.mainloop()
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
# GUI画面の表示
|
540
|
-
|
541
|
-
create_gui()
|
542
|
-
|
543
|
-
|
544
|
-
|
545
13
|
|
546
14
|
|
547
15
|
```
|