回答編集履歴
3
追記
test
CHANGED
@@ -134,9 +134,9 @@
|
|
134
134
|
|
135
135
|
|
136
136
|
|
137
|
-
0. イベント…同じ種類の物(ラベル、テキストボックス)などに対して同じ処理を
|
137
|
+
0. イベント…同じ種類の物(ラベル、テキストボックス)などに対してイベント発生時に同じ処理を行いたい時。
|
138
|
-
|
138
|
+
|
139
|
-
引数にイベント
|
139
|
+
引数にイベント発生元が渡されるので、同じ処理をできます。
|
140
140
|
|
141
141
|
|
142
142
|
|
2
from functools import partial
test
CHANGED
@@ -119,3 +119,149 @@
|
|
119
119
|
|
120
120
|
|
121
121
|
```
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
---
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
> クラス内でメソッド間でボタン等の値を取得させようと思ったら、ローカル変数のような書き方は、始めからしない方が良いのでしょうか?
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
適材適所かと。私なら画面項目(パネル、ラベル、ボタン)はローカル変数にはしないです。
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
0. イベント…同じ種類の物(ラベル、テキストボックス)などに対して同じ処理をしたい時。
|
138
|
+
|
139
|
+
引数にイベントオブジェクトが渡されるので、同じ処理をできます。
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
0. インスタンス変数…イベント発生元以外の項目に対してなんらかの処理を行いたい時(質問文の場合)
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
回答には書きませんでしたが、3,functools#partialを使った手法もあります。
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
```Python
|
152
|
+
|
153
|
+
# -*- coding: utf-8 -*-
|
154
|
+
|
155
|
+
import wx
|
156
|
+
|
157
|
+
import os
|
158
|
+
|
159
|
+
from functools import partial
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
class MainFrame(wx.Frame):
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
def __init__(self):
|
168
|
+
|
169
|
+
wx.Frame.__init__(self, None, wx.ID_ANY, 'logplot')
|
170
|
+
|
171
|
+
panel = wx.Panel(self, wx.ID_ANY)
|
172
|
+
|
173
|
+
panel.SetBackgroundColour(wx.Colour(128, 128, 128))
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
#button
|
178
|
+
|
179
|
+
button_1 = wx.ToggleButton(panel, wx.ID_ANY, 'Line/Log')
|
180
|
+
|
181
|
+
button_1.Bind(wx.EVT_TOGGLEBUTTON, self.button_click)
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
# comboboxとbuttonの宣言順を変えている点に注意
|
186
|
+
|
187
|
+
#combobox
|
188
|
+
|
189
|
+
combobox_1 = wx.ComboBox(panel, wx.ID_ANY, 'select ScanList', choices = ("1","2","3"), style = wx.CB_READONLY)
|
190
|
+
|
191
|
+
combobox_1.Bind(wx.EVT_TEXT, partial(self.Ontext, btn=button_1))
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
#Text
|
196
|
+
|
197
|
+
text_1 = wx.StaticText(panel, wx.ID_ANY, )
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
vbox_1 = wx.BoxSizer(wx.VERTICAL)
|
202
|
+
|
203
|
+
vbox_1.Add(combobox_1, 0, wx.LEFT | wx.RIGHT, 10)
|
204
|
+
|
205
|
+
vbox_1.Add(button_1, 0, wx.RIGHT, 10)
|
206
|
+
|
207
|
+
vbox_1.Add(text_1, 0, wx.RIGHT, 10)
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
panel.SetSizer(vbox_1)
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
def button_click(self, event):
|
216
|
+
|
217
|
+
button_1 = event.GetEventObject()
|
218
|
+
|
219
|
+
if button_1.GetValue():
|
220
|
+
|
221
|
+
button_1.SetLabel("Log")
|
222
|
+
|
223
|
+
else:
|
224
|
+
|
225
|
+
button_1.SetLabel("Line")
|
226
|
+
|
227
|
+
print(button_1.GetValue())
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
def Ontext(self, event, btn):
|
232
|
+
|
233
|
+
print(btn.GetValue())
|
234
|
+
|
235
|
+
combobox_1 = event.GetEventObject()
|
236
|
+
|
237
|
+
print(combobox_1.GetValue())
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
#モジュール空間の汚染を避けるためにスコープを切る
|
244
|
+
|
245
|
+
def main() -> None:
|
246
|
+
|
247
|
+
app = wx.App()
|
248
|
+
|
249
|
+
MainFrame().Show()
|
250
|
+
|
251
|
+
app.MainLoop()
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
if __name__ == "__main__":
|
258
|
+
|
259
|
+
main()
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
```
|
264
|
+
|
265
|
+
◇参考情報
|
266
|
+
|
267
|
+
[functools.partial](https://docs.python.jp/3/library/functools.html#functools.partial)
|
1
追記
test
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
|
7
7
|
|
8
8
|
|
9
|
-
スコープを
|
9
|
+
変数:`button_1`のスコープをローカル変数からインスタンス変数に変更すればよいかと。
|
10
10
|
|
11
11
|
|
12
12
|
|