teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

2019/01/15 23:33

投稿

ChaCha_MaRu
ChaCha_MaRu

スコア15

title CHANGED
File without changes
body CHANGED
@@ -4,271 +4,5 @@
4
4
 
5
5
  ```python3
6
6
  # -*- coding: utf-8 -*-
7
- from tkinter import *
8
- import tkinter as tk
9
- # python2の場合は、import Tkinter as tk
10
- import tkinter.ttk as ttk
11
- # python2の場合は、import ttk
12
- import sqlite3
13
- import sys
14
7
 
15
- # ログイン画面関数
16
- def login_gui():
17
- #ログインの認証
18
- def foo():
19
- #正規のユーザーの時create_gui(メイン画面)へ
20
- if t.get() == "hello":
21
- root.destroy()
22
- create_gui()
23
- #ユーザ名が違うときpass
24
- else:
25
- pass
26
-
27
- # root定義
28
- root = tk.Tk()
29
- root.title('login')
30
- frame1 = tk.Frame(root)
31
- label1 = tk.Label(frame1, text='Your name:')
32
-
33
- #入力された文字の取得
34
- t = tk.StringVar()
35
-
36
- #メニュー定義
37
- entry1 = tk.Entry(frame1, textvariable=t)
38
- button1 = tk.Button(frame1, text='OK', command=foo)
39
-
40
- frame1.grid(row=0,column=0,sticky=(N,E,S,W))
41
- label1.grid(row=1,column=1,sticky=E)
42
- entry1.grid(row=1,column=2,sticky=W)
43
- button1.grid(row=2,column=2,sticky=W)
44
-
45
- for child in frame1.winfo_children():
46
- child.grid_configure(padx=5, pady=5)
47
-
48
- root.mainloop()
49
-
50
-
51
-
52
-
53
- # 登録画面のGUI
54
- def create_gui():
55
- # ----------------------------------------
56
- # コールバック関数群
57
- # ----------------------------------------
58
- # 表示ボタンが押下されたときのコールバック関数
59
- def select_button():
60
- root.destroy()
61
- select_gui()
62
- # ----------------------------------------
63
- # 終了ボタンが押下されたときのコールバック関数
64
- def quit_button():
65
- root.destroy()
66
- # ----------------------------------------
67
- # 登録ボタンがクリックされた時にデータをDBに登録するコールバック関数
68
- def create_sql(item_name):
69
-
70
- # データベースに接続
71
- c = sqlite3.connect("database.db")
72
- # item_nameをWHERE句に渡してitem_codeを取得する
73
- item_code = c.execute("""
74
- SELECT item_code FROM item
75
- WHERE item_name = '{}'
76
- """.format(item_name))
77
- item_code = item_code.fetchone()[0]
78
- # 日付の読み取り
79
- acc_data = entry1.get().replace("/","-")
80
- # 金額の読み取り
81
- amount = entry3.get()
82
-
83
- # SQLを発行してDBへ登録
84
- # python2の場合は、ユニコード文字列でsqlite3に渡す
85
- # また、コミットする場合は、commitメソッドを用いる
86
- try:
87
- c.execute("""
88
- INSERT INTO acc_data(acc_date,item_code,amount)
89
- VALUES('{}',{},{});
90
- """.format(acc_data,item_code,amount))
91
- c.execute("COMMIT;")
92
- print("1件登録しました")
93
- # ドメインエラーなどにより登録できなかった場合のエラー処理
94
- except:
95
- print("エラーにより登録できませんでした")
96
- # ----------------------------------------
97
- # 内訳テーブル(item)にあるitem_nameのタプルを作成する
98
- def createitemname():
99
- # データベースの接続
100
- c = sqlite3.connect("database.db")
101
- # 空の「リスト型」を定義
102
- li = []
103
- # SELECT文を発行し、item_nameを取得し、for文で回す
104
- for r in c.execute("SELECT item_name FROM item"):
105
- # item_nameをリストに追加する
106
- li.append(r)
107
- # リスト型のliをタプル型に変換して、ファンクションに戻す
108
- return tuple(li)
109
- # ----------------------------------------
110
-
111
- # 空のデータベースを作成して接続する
112
- dbname = "database.db"
113
- c = sqlite3.connect(dbname)
114
- c.execute("PRAGMA foreign_keys = 1")
115
-
116
- # 既にデータベースが登録されている場合は、ddlの発行でエラーが出るのでexceptブロックで回避する
117
- try:
118
- # itemテーブルの定義
119
- ddl = """
120
- CREATE TABLE item
121
- (
122
- item_code INTEGER PRIMARY KEY AUTOINCREMENT,
123
- item_name TEXT NOT NULL UNIQUE
124
- )
125
- """
126
- # SQLの発行
127
- c.execute(ddl)
128
- # acc_dataテーブルの定義
129
- ddl = """
130
- CREATE TABLE acc_data
131
- (
132
- id INTEGER PRIMARY KEY AUTOINCREMENT,
133
- acc_date DATE NOT NULL,
134
- item_code INTEGER NOT NULL,
135
- amount INTEGER,
136
- FOREIGN KEY(item_code) REFERENCES item(item_code)
137
- )
138
- """
139
- # itemテーブルへリファレンスデータの登録
140
- # 初回起動時に書き換えることで変更可能
141
- c.execute(ddl)
142
- c.execute("INSERT INTO item VALUES(1,'食費')")
143
- c.execute("INSERT INTO item VALUES(2,'住宅費')")
144
- c.execute("INSERT INTO item VALUES(3,'光熱費')")
145
- c.execute("COMMIT")
146
- except:
147
- pass
148
-
149
- # rootフレームの設定
150
- root = tk.Tk()
151
- root.title("家計簿アプリ")
152
- root.geometry("300x280")
153
-
154
- # メニューの設定
155
- frame = tk.Frame(root,bd=2,relief="ridge")
156
- frame.pack(fill="x")
157
- button1 = tk.Button(frame,text="入力")
158
- button1.pack(side="left")
159
- button2 = tk.Button(frame,text="表示",command=select_button)
160
- button2.pack(side="left")
161
- button3 = tk.Button(frame,text="終了",command=quit_button)
162
- button3.pack(side="right")
163
-
164
- # 入力画面ラベルの設定
165
- label1 = tk.Label(root,text="【入力画面】",font=("",16),height=2)
166
- label1.pack(fill="x")
167
-
168
- # 日付のラベルとエントリーの設定
169
- frame1 = tk.Frame(root,pady=10)
170
- frame1.pack()
171
- label2 = tk.Label(frame1,font=("",14),text="日付")
172
- label2.pack(side="left")
173
- entry1 = tk.Entry(frame1,font=("",14),justify="center",width=15)
174
- entry1.pack(side="left")
175
-
176
- # 内訳のラベルとエントリーの設定
177
- frame2 = tk.Frame(root,pady=10)
178
- frame2.pack()
179
- label3 = tk.Label(frame2,font=("",14),text="内訳")
180
- label3.pack(side="left")
181
- # 内訳コンボボックスの作成
182
- combo = ttk.Combobox(frame2, state='readonly',font=("",14),width=13)
183
- combo["values"] = createitemname()
184
- combo.current(0)
185
- combo.pack()
186
-
187
- # 金額のラベルとエントリーの設定
188
- frame3 = tk.Frame(root,pady=10)
189
- frame3.pack()
190
- label4 = tk.Label(frame3,font=("",14),text="金額")
191
- label4.pack(side="left")
192
- entry3 = tk.Entry(frame3,font=("",14),justify="center",width=15)
193
- entry3.pack(side="left")
194
-
195
- # 登録ボタンの設定
196
- button4 = tk.Button(root,text="登録",
197
- font=("",16),
198
- width=10,bg="gray",
199
- command=lambda:create_sql(combo.get()))
200
- button4.pack()
201
-
202
- root.mainloop()
203
-
204
- login_gui()
205
-
206
-
207
-
208
-
209
- # 表示画面のGUI
210
- def select_gui():
211
- # ----------------------------------------
212
- # コールバック関数群
213
- # ----------------------------------------
214
- # 登録ボタンが押下されたときのコールバック関数
215
- def create_button():
216
- root.destroy()
217
- create_gui()
218
- # ----------------------------------------
219
- # 終了ボタンが押下されたときのコールバック関数
220
- def quit_button():
221
- root.destroy()
222
- # ----------------------------------------
223
-
224
- # rootフレームの設定
225
- root = tk.Tk()
226
- root.title("家計簿アプリ")
227
- root.geometry("700x700")
228
-
229
- # メニューの設定
230
- frame = tk.Frame(root,bd=2,relief="ridge")
231
- frame.pack(fill="x")
232
- button1 = tk.Button(frame,text="入力",command=create_button)
233
- button1.pack(side="left")
234
- button2 = tk.Button(frame,text="表示")
235
- button2.pack(side="left")
236
- button3 = tk.Button(frame,text="終了",command=quit_button)
237
- button3.pack(side="right")
238
-
239
-
240
- # 入力画面ラベルの設定
241
- label1 = tk.Label(root,text="【表示画面】",font=("",16),height=2)
242
- label1.pack(fill="x")
243
- chk=tk.BooleanVar()
244
- chk.set(True)
245
- chko=tk.BooleanVar()
246
- chko.set(False)
247
- chk1=tk.Checkbutton(root,text='収入を表示',variable=chk)
248
- chk1.pack()
249
- chk2=tk.Checkbutton(root,text='支出を表示',variable=chko)
250
- chk2.pack()
251
-
252
-
253
-
254
- # 期間選択のラベルエントリーの設定
255
- frame1 = tk.Frame(root,pady=15)
256
- frame1.pack()
257
- label2 = tk.Label(frame1,font=("",14),text="期間 ")
258
- label2.pack(side="left")
259
- entry1 = tk.Entry(frame1,font=("",14),justify="center",width=12)
260
- entry1.pack(side="left")
261
- label3 = tk.Label(frame1,font=("",14),text=" ~ ")
262
- label3.pack(side="left")
263
- entry2 = tk.Entry(frame1,font=("",14),justify="center",width=12)
264
- entry2.pack(side="left")
265
-
266
- # メインループ
267
- root.mainloop()
268
-
269
-
270
- # GUI画面の表示
271
- create_gui()
272
-
273
-
274
8
  ```