回答編集履歴
7
文体修正
test
CHANGED
@@ -232,4 +232,4 @@
|
|
232
232
|
|
233
233
|
execute関数は、渡されたデータを使って実行処理を担当。
|
234
234
|
|
235
|
-
実行ボタンが押されたときに呼ばれ
|
235
|
+
実行ボタンが押されたときに呼ばれます。
|
6
説明追記
test
CHANGED
@@ -231,3 +231,5 @@
|
|
231
231
|
|
232
232
|
|
233
233
|
execute関数は、渡されたデータを使って実行処理を担当。
|
234
|
+
|
235
|
+
実行ボタンが押されたときに呼ばれる。
|
5
説明追記
test
CHANGED
@@ -213,3 +213,21 @@
|
|
213
213
|
main()
|
214
214
|
|
215
215
|
```
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
main関数はアプリケーション担当。
|
220
|
+
|
221
|
+
GUIとデータを用意して描画処理やイベント処理(mainloop)をします。
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
setup関数はGUI構築担当。構築以外の仕事はしません。
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
Dataクラスはデータ担当。整合性確認などデータに関する処理だけを担当。
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
execute関数は、渡されたデータを使って実行処理を担当。
|
4
executeを関数化
test
CHANGED
@@ -66,7 +66,7 @@
|
|
66
66
|
|
67
67
|
|
68
68
|
|
69
|
-
def
|
69
|
+
def validate(self):
|
70
70
|
|
71
71
|
date = self.date.get()
|
72
72
|
|
@@ -74,7 +74,7 @@
|
|
74
74
|
|
75
75
|
msgbox.showwarning(message="date format error")
|
76
76
|
|
77
|
-
return
|
77
|
+
return False
|
78
78
|
|
79
79
|
|
80
80
|
|
@@ -84,17 +84,29 @@
|
|
84
84
|
|
85
85
|
msgbox.showwarning(message="file not found")
|
86
86
|
|
87
|
+
return False
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
return True
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
def execute(data):
|
98
|
+
|
99
|
+
if not data.validate():
|
100
|
+
|
87
|
-
|
101
|
+
return
|
88
|
-
|
89
|
-
|
90
|
-
|
102
|
+
|
91
|
-
|
103
|
+
print("ftype:", repr(data.ftype.get()))
|
92
|
-
|
104
|
+
|
93
|
-
|
105
|
+
print("date:", repr(data.date.get()))
|
94
|
-
|
106
|
+
|
95
|
-
|
107
|
+
print("filename:", repr(data.filename.get()))
|
96
|
-
|
108
|
+
|
97
|
-
|
109
|
+
print("num:", repr(data.num.get()))
|
98
110
|
|
99
111
|
|
100
112
|
|
@@ -172,7 +184,7 @@
|
|
172
184
|
|
173
185
|
|
174
186
|
|
175
|
-
w = Button(view, text='実 行 ', width=16, command=da
|
187
|
+
w = Button(view, text='実 行 ', width=16, command=lambda: execute(data))
|
176
188
|
|
177
189
|
w.grid(row=4, column=4, cnf=pad, sticky=tk.W)
|
178
190
|
|
3
関数名変更
test
CHANGED
@@ -100,7 +100,7 @@
|
|
100
100
|
|
101
101
|
|
102
102
|
|
103
|
-
def
|
103
|
+
def setup(view, data):
|
104
104
|
|
105
105
|
view.propagate(False)
|
106
106
|
|
@@ -188,7 +188,7 @@
|
|
188
188
|
|
189
189
|
app.resizable(False, False)
|
190
190
|
|
191
|
-
|
191
|
+
setup(tk.Frame(app), Data())
|
192
192
|
|
193
193
|
app.mainloop()
|
194
194
|
|
2
変数名modelをdataに変更
test
CHANGED
@@ -100,7 +100,7 @@
|
|
100
100
|
|
101
101
|
|
102
102
|
|
103
|
-
def build(view,
|
103
|
+
def build(view, data):
|
104
104
|
|
105
105
|
view.propagate(False)
|
106
106
|
|
@@ -116,17 +116,13 @@
|
|
116
116
|
|
117
117
|
w = Radiobutton(view, text='Shipment Report',
|
118
118
|
|
119
|
-
variable=
|
119
|
+
value=1, variable=data.ftype)
|
120
|
-
|
121
|
-
value=1)
|
122
120
|
|
123
121
|
w.grid(row=0, column=1, cnf=pad)
|
124
122
|
|
125
123
|
w = Radiobutton(view, text='New Product Shipment Report',
|
126
124
|
|
127
|
-
variable=
|
125
|
+
value=2, variable=data.ftype)
|
128
|
-
|
129
|
-
value=2)
|
130
126
|
|
131
127
|
w.grid(row=0, column=2, cnf=pad, sticky=tk.W)
|
132
128
|
|
@@ -136,9 +132,9 @@
|
|
136
132
|
|
137
133
|
w.grid(row=1, column=0, cnf=pad)
|
138
134
|
|
139
|
-
vcmd = (view.register(
|
135
|
+
vcmd = (view.register(data.is_valid_date), '%P')
|
140
|
-
|
136
|
+
|
141
|
-
w = Entry(view, textvariable=
|
137
|
+
w = Entry(view, textvariable=data.date)
|
142
138
|
|
143
139
|
w.configure(validate="key", validatecommand=vcmd)
|
144
140
|
|
@@ -150,13 +146,13 @@
|
|
150
146
|
|
151
147
|
w.grid(row=2, column=0, cnf=pad)
|
152
148
|
|
153
|
-
w = Entry(view, textvariable=
|
149
|
+
w = Entry(view, textvariable=data.filename)
|
154
150
|
|
155
151
|
w.grid(row=2, column=1, columnspan=3, cnf=pad, sticky=tk.E + tk.W)
|
156
152
|
|
157
153
|
|
158
154
|
|
159
|
-
w = Button(view, text='参 照', width=16, command=
|
155
|
+
w = Button(view, text='参 照', width=16, command=data.select_file)
|
160
156
|
|
161
157
|
w.grid(row=2, column=4, cnf=pad, sticky=tk.W)
|
162
158
|
|
@@ -166,7 +162,7 @@
|
|
166
162
|
|
167
163
|
w.grid(row=3, column=0, cnf=pad)
|
168
164
|
|
169
|
-
w = Entry(view, textvariable=
|
165
|
+
w = Entry(view, textvariable=data.num)
|
170
166
|
|
171
167
|
w.grid(row=3, column=1, cnf=pad, sticky=tk.W)
|
172
168
|
|
@@ -176,7 +172,7 @@
|
|
176
172
|
|
177
173
|
|
178
174
|
|
179
|
-
w = Button(view, text='実 行 ', width=16, command=
|
175
|
+
w = Button(view, text='実 行 ', width=16, command=data.execute)
|
180
176
|
|
181
177
|
w.grid(row=4, column=4, cnf=pad, sticky=tk.W)
|
182
178
|
|
1
処理を見直したコードを添付
test
CHANGED
@@ -7,3 +7,201 @@
|
|
7
7
|
tkinterを一切使わず、ファイル名を引数に取ってexcelを読み出す関数を作れますか?
|
8
8
|
|
9
9
|
そのあと、tkinterのGUI処理からその関数を呼び出すようにすればいいです。
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
ファイル名を決定するまでの処理を整理してみました。
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
```python
|
18
|
+
|
19
|
+
import tkinter as tk
|
20
|
+
|
21
|
+
from tkinter.ttk import *
|
22
|
+
|
23
|
+
from tkinter.filedialog import askopenfilename
|
24
|
+
|
25
|
+
from tkinter import messagebox as msgbox
|
26
|
+
|
27
|
+
import os
|
28
|
+
|
29
|
+
import re
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
class Data:
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
def __init__(self):
|
40
|
+
|
41
|
+
self.ftype = tk.IntVar(value=1)
|
42
|
+
|
43
|
+
self.date = tk.StringVar()
|
44
|
+
|
45
|
+
self.filename = tk.StringVar()
|
46
|
+
|
47
|
+
self.num = tk.StringVar()
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
def is_valid_date(self, text):
|
52
|
+
|
53
|
+
return (not text) or len(text) <= 6 and text.isdecimal()
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
def select_file(self):
|
58
|
+
|
59
|
+
path = askopenfilename(initialdir=r'C:\Python',
|
60
|
+
|
61
|
+
filetypes=[('Excel File', '*xlsx')])
|
62
|
+
|
63
|
+
if path:
|
64
|
+
|
65
|
+
self.filename.set(path)
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
def execute(self):
|
70
|
+
|
71
|
+
date = self.date.get()
|
72
|
+
|
73
|
+
if not re.match("[0-9]{6}", date):
|
74
|
+
|
75
|
+
msgbox.showwarning(message="date format error")
|
76
|
+
|
77
|
+
return
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
filename = self.filename.get()
|
82
|
+
|
83
|
+
if not os.path.exists(filename):
|
84
|
+
|
85
|
+
msgbox.showwarning(message="file not found")
|
86
|
+
|
87
|
+
return
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
print("ftype:", repr(self.ftype.get()))
|
92
|
+
|
93
|
+
print("date:", repr(date))
|
94
|
+
|
95
|
+
print("filename:", repr(filename))
|
96
|
+
|
97
|
+
print("num:", repr(self.num.get()))
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
def build(view, model):
|
104
|
+
|
105
|
+
view.propagate(False)
|
106
|
+
|
107
|
+
view.pack(expand=True, fill=tk.BOTH)
|
108
|
+
|
109
|
+
pad = {"padx": 10, "pady": 10}
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
w = Label(view, text='取込ファイル種別')
|
114
|
+
|
115
|
+
w.grid(row=0, column=0, cnf=pad)
|
116
|
+
|
117
|
+
w = Radiobutton(view, text='Shipment Report',
|
118
|
+
|
119
|
+
variable=model.ftype,
|
120
|
+
|
121
|
+
value=1)
|
122
|
+
|
123
|
+
w.grid(row=0, column=1, cnf=pad)
|
124
|
+
|
125
|
+
w = Radiobutton(view, text='New Product Shipment Report',
|
126
|
+
|
127
|
+
variable=model.ftype,
|
128
|
+
|
129
|
+
value=2)
|
130
|
+
|
131
|
+
w.grid(row=0, column=2, cnf=pad, sticky=tk.W)
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
w = Label(view, text='取込年月')
|
136
|
+
|
137
|
+
w.grid(row=1, column=0, cnf=pad)
|
138
|
+
|
139
|
+
vcmd = (view.register(model.is_valid_date), '%P')
|
140
|
+
|
141
|
+
w = Entry(view, textvariable=model.date)
|
142
|
+
|
143
|
+
w.configure(validate="key", validatecommand=vcmd)
|
144
|
+
|
145
|
+
w.grid(row=1, column=1, cnf=pad, sticky=tk.W)
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
w = Label(view, text='取込ファイル')
|
150
|
+
|
151
|
+
w.grid(row=2, column=0, cnf=pad)
|
152
|
+
|
153
|
+
w = Entry(view, textvariable=model.filename)
|
154
|
+
|
155
|
+
w.grid(row=2, column=1, columnspan=3, cnf=pad, sticky=tk.E + tk.W)
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
w = Button(view, text='参 照', width=16, command=model.select_file)
|
160
|
+
|
161
|
+
w.grid(row=2, column=4, cnf=pad, sticky=tk.W)
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
w = Label(view, text='取込件数')
|
166
|
+
|
167
|
+
w.grid(row=3, column=0, cnf=pad)
|
168
|
+
|
169
|
+
w = Entry(view, textvariable=model.num)
|
170
|
+
|
171
|
+
w.grid(row=3, column=1, cnf=pad, sticky=tk.W)
|
172
|
+
|
173
|
+
w = Label(view, text='件')
|
174
|
+
|
175
|
+
w.grid(row=3, column=2, cnf=pad, sticky=tk.W)
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
w = Button(view, text='実 行 ', width=16, command=model.execute)
|
180
|
+
|
181
|
+
w.grid(row=4, column=4, cnf=pad, sticky=tk.W)
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
def main():
|
188
|
+
|
189
|
+
app = tk.Tk()
|
190
|
+
|
191
|
+
app.title('データインポートプログラム')
|
192
|
+
|
193
|
+
app.resizable(False, False)
|
194
|
+
|
195
|
+
build(tk.Frame(app), Data())
|
196
|
+
|
197
|
+
app.mainloop()
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
if __name__ == "__main__":
|
204
|
+
|
205
|
+
main()
|
206
|
+
|
207
|
+
```
|