質問編集履歴

2

初心者マークをつけ忘れました

2020/08/13 11:50

投稿

kamome01
kamome01

スコア16

test CHANGED
File without changes
test CHANGED
@@ -270,6 +270,6 @@
270
270
 
271
271
  まだ未実装の部分もあり、混乱を招く変数などもあるかもしれませんが、
272
272
 
273
- チェックボタンが1つしか表示されない問題点についてアドバイスいただきたいです。
273
+ チェックボタンが1つしか表示されない問題点についてアドバイスいただきたいです。
274
274
 
275
275
  よろしくお願いします。

1

画像が表示されていなかったので編集しました

2020/08/13 11:50

投稿

kamome01
kamome01

スコア16

test CHANGED
File without changes
test CHANGED
@@ -16,256 +16,252 @@
16
16
 
17
17
  その下に、4つのチェックボタンを配置したいのですが、なぜか1のみ表示されます。
18
18
 
19
+
20
+
21
+ ![イメージ説明](9967cc7d9b4e40ed75537a5eade66c16.png)
22
+
23
+
24
+
25
+ ### 該当のソースコード
26
+
27
+
28
+
29
+ ```python
30
+
31
+ import tkinter
32
+
33
+ import tkinter.ttk
34
+
35
+ from PIL import Image, ImageTk
36
+
37
+ import random
38
+
39
+
40
+
41
+ class Application(tkinter.Frame):
42
+
43
+ def __init__(self, master=None):
44
+
45
+ super().__init__(master)
46
+
47
+ self.master = master
48
+
49
+ self.master.title('test')
50
+
51
+ self.pack()
52
+
53
+ self.create_widgets()
54
+
55
+ self.count=0
56
+
57
+ self.correct_No=0
58
+
59
+ self.answer_No=0
60
+
61
+ self.correct_count=0
62
+
63
+ self.rate=0
64
+
65
+
66
+
67
+ def create_widgets(self):
68
+
69
+ # ボタン
70
+
71
+ self.next_button = tkinter.ttk.Button(self, text='next')
72
+
73
+ self.next_button.grid(row=4,column=0)
74
+
75
+ self.next_button.bind('<Button-1>', change_graphics)
76
+
77
+
78
+
79
+ self.button1 = tkinter.ttk.Checkbutton(self, text='1')
80
+
81
+ self.button1.grid(row=3, column=0)
82
+
83
+ self.button1.bind('<Button-1>')
84
+
85
+ self.button2 = tkinter.ttk.Checkbutton(self, text='2')
86
+
87
+ self.button2.grid(row=3, column=1)
88
+
89
+ self.button2.bind('<Button-1>')
90
+
91
+ self.button3 = tkinter.ttk.Checkbutton(self, text='3')
92
+
93
+ self.button3.grid(row=3, column=2)
94
+
95
+ self.button3.bind('<Button-1>')
96
+
97
+ self.button4 = tkinter.ttk.Checkbutton(self, text='4')
98
+
99
+ self.button4.grid(row=3, column=3)
100
+
101
+ self.button4.bind('<Button-1>')
102
+
103
+
104
+
105
+
106
+
107
+ # canvas
108
+
109
+ self.test_canvas = tkinter.Canvas(self, width=1080, height=540)
110
+
111
+ self.test_canvas.grid(row=0, column=0, rowspan=3)
112
+
113
+
114
+
115
+ self.test_canvas2 = tkinter.Canvas(self, width=1080, height=540)
116
+
117
+ self.test_canvas2.grid(row=0, column=1, rowspan=3)
118
+
119
+
120
+
121
+ self.test_canvas3 = tkinter.Canvas(self, width=1080, height=540)
122
+
123
+ self.test_canvas3.grid(row=0, column=2, rowspan=3)
124
+
125
+
126
+
127
+ self.test_canvas4 = tkinter.Canvas(self, width=1080, height=540)
128
+
129
+ self.test_canvas4.grid(row=0, column=3, rowspan=3)
130
+
131
+
132
+
133
+ # canvasに初期画像を表示
134
+
135
+ self.test_canvas.photo = ImageTk.PhotoImage(original_image)
136
+
137
+ self.image_on_canvas = self.test_canvas.create_image(1080/16, 120, anchor='nw', image=self.test_canvas.photo)
138
+
139
+
140
+
141
+ self.test_canvas2.photo = ImageTk.PhotoImage(original_image2)
142
+
143
+ self.image_on_canvas2 = self.test_canvas.create_image(250+1080/16, 120, anchor='nw', image=self.test_canvas2.photo)
144
+
145
+
146
+
147
+ self.test_canvas3.photo = ImageTk.PhotoImage(original_image3)
148
+
149
+ self.image_on_canvas3 = self.test_canvas.create_image(2*250+1080/16, 120, anchor='nw', image=self.test_canvas3.photo)
150
+
151
+
152
+
153
+ self.test_canvas4.photo = ImageTk.PhotoImage(original_image4)
154
+
155
+ self.image_on_canvas4 = self.test_canvas.create_image(3*250+1080/16, 120, anchor='nw', image=self.test_canvas4.photo)
156
+
157
+
158
+
159
+ def set_image(img):
160
+
161
+ # canvasの書き換え
162
+
163
+ app.test_canvas.photo = ImageTk.PhotoImage(img)
164
+
165
+ app.test_canvas.create_image(500,120,anchor='nw', image=app.test_canvas.photo)
166
+
167
+
168
+
169
+ app.test_canvas2.photo = ImageTk.PhotoImage(img)
170
+
171
+ app.test_canvas3.photo = ImageTk.PhotoImage(img)
172
+
173
+ app.test_canvas4.photo = ImageTk.PhotoImage(img)
174
+
175
+
176
+
177
+
178
+
179
+ def set_image4(img1,img2,img3,img4):
180
+
181
+ app.test_canvas.photo = ImageTk.PhotoImage(img1)
182
+
183
+ app.test_canvas.itemconfig(app.image_on_canvas, image=app.test_canvas.photo)
184
+
185
+ app.test_canvas2.photo = ImageTk.PhotoImage(img2)
186
+
187
+ app.test_canvas.itemconfig(app.image_on_canvas2, image=app.test_canvas2.photo)
188
+
189
+ app.test_canvas3.photo = ImageTk.PhotoImage(img3)
190
+
191
+ app.test_canvas.itemconfig(app.image_on_canvas3, image=app.test_canvas3.photo)
192
+
193
+ app.test_canvas4.photo = ImageTk.PhotoImage(img4)
194
+
195
+ app.test_canvas.itemconfig(app.image_on_canvas4, image=app.test_canvas4.photo)
196
+
197
+
198
+
199
+
200
+
201
+ def change_graphics(event):
202
+
203
+ app.count+=1
204
+
205
+ R=random.randint(1,260)
206
+
207
+ R2=random.randint(1,260)
208
+
209
+ R3=random.randint(1,260)
210
+
211
+ R4=random.randint(1,260)
212
+
213
+ original_image= Image.open(open('sample'+str(R)+'.png', 'rb'))
214
+
215
+ original_image2= Image.open(open('sample'+str(R2)+'.png', 'rb'))
216
+
217
+ original_image3= Image.open(open('sample'+str(R3)+'.png', 'rb'))
218
+
219
+ original_image4= Image.open(open('sample'+str(R4)+'.png', 'rb'))
220
+
221
+ if(app.count%2==0):
222
+
223
+ set_image4(original_image,original_image2,original_image3,original_image4)
224
+
225
+ else:
226
+
227
+ set_image(original_image)
228
+
229
+
230
+
231
+
232
+
233
+ # 画像ファイル読み込み
234
+
235
+ R=random.randint(1,260)
236
+
237
+ R2=random.randint(1,260)
238
+
239
+ R3=random.randint(1,260)
240
+
241
+ R4=random.randint(1,260)
242
+
243
+ original_image = Image.open('black.jpg')
244
+
245
+ original_image2 = Image.open('black.jpg')
246
+
247
+ original_image3 = Image.open('black.jpg')
248
+
249
+ original_image4 = Image.open('black.jpg')
250
+
251
+
252
+
253
+ # アプリケーション起動
254
+
255
+ root = tkinter.Tk()
256
+
257
+ app = Application(master=root)
258
+
259
+ app.mainloop()
260
+
261
+
262
+
19
263
  ```
20
264
 
21
- ![イメージ説明](6af005738583fd7861797f0f427653e4.png)
22
-
23
- ```
24
-
25
-
26
-
27
-
28
-
29
- ### 該当のソースコード
30
-
31
-
32
-
33
- ```python
34
-
35
- import tkinter
36
-
37
- import tkinter.ttk
38
-
39
- from PIL import Image, ImageTk
40
-
41
- import random
42
-
43
-
44
-
45
- class Application(tkinter.Frame):
46
-
47
- def __init__(self, master=None):
48
-
49
- super().__init__(master)
50
-
51
- self.master = master
52
-
53
- self.master.title('test')
54
-
55
- self.pack()
56
-
57
- self.create_widgets()
58
-
59
- self.count=0
60
-
61
- self.correct_No=0
62
-
63
- self.answer_No=0
64
-
65
- self.correct_count=0
66
-
67
- self.rate=0
68
-
69
-
70
-
71
- def create_widgets(self):
72
-
73
- # ボタン
74
-
75
- self.next_button = tkinter.ttk.Button(self, text='next')
76
-
77
- self.next_button.grid(row=4,column=0)
78
-
79
- self.next_button.bind('<Button-1>', change_graphics)
80
-
81
-
82
-
83
- self.button1 = tkinter.ttk.Checkbutton(self, text='1')
84
-
85
- self.button1.grid(row=3, column=0)
86
-
87
- self.button1.bind('<Button-1>')
88
-
89
- self.button2 = tkinter.ttk.Checkbutton(self, text='2')
90
-
91
- self.button2.grid(row=3, column=1)
92
-
93
- self.button2.bind('<Button-1>')
94
-
95
- self.button3 = tkinter.ttk.Checkbutton(self, text='3')
96
-
97
- self.button3.grid(row=3, column=2)
98
-
99
- self.button3.bind('<Button-1>')
100
-
101
- self.button4 = tkinter.ttk.Checkbutton(self, text='4')
102
-
103
- self.button4.grid(row=3, column=3)
104
-
105
- self.button4.bind('<Button-1>')
106
-
107
-
108
-
109
-
110
-
111
- # canvas
112
-
113
- self.test_canvas = tkinter.Canvas(self, width=1080, height=540)
114
-
115
- self.test_canvas.grid(row=0, column=0, rowspan=3)
116
-
117
-
118
-
119
- self.test_canvas2 = tkinter.Canvas(self, width=1080, height=540)
120
-
121
- self.test_canvas2.grid(row=0, column=1, rowspan=3)
122
-
123
-
124
-
125
- self.test_canvas3 = tkinter.Canvas(self, width=1080, height=540)
126
-
127
- self.test_canvas3.grid(row=0, column=2, rowspan=3)
128
-
129
-
130
-
131
- self.test_canvas4 = tkinter.Canvas(self, width=1080, height=540)
132
-
133
- self.test_canvas4.grid(row=0, column=3, rowspan=3)
134
-
135
-
136
-
137
- # canvasに初期画像を表示
138
-
139
- self.test_canvas.photo = ImageTk.PhotoImage(original_image)
140
-
141
- self.image_on_canvas = self.test_canvas.create_image(1080/16, 120, anchor='nw', image=self.test_canvas.photo)
142
-
143
-
144
-
145
- self.test_canvas2.photo = ImageTk.PhotoImage(original_image2)
146
-
147
- self.image_on_canvas2 = self.test_canvas.create_image(250+1080/16, 120, anchor='nw', image=self.test_canvas2.photo)
148
-
149
-
150
-
151
- self.test_canvas3.photo = ImageTk.PhotoImage(original_image3)
152
-
153
- self.image_on_canvas3 = self.test_canvas.create_image(2*250+1080/16, 120, anchor='nw', image=self.test_canvas3.photo)
154
-
155
-
156
-
157
- self.test_canvas4.photo = ImageTk.PhotoImage(original_image4)
158
-
159
- self.image_on_canvas4 = self.test_canvas.create_image(3*250+1080/16, 120, anchor='nw', image=self.test_canvas4.photo)
160
-
161
-
162
-
163
- def set_image(img):
164
-
165
- # canvasの書き換え
166
-
167
- app.test_canvas.photo = ImageTk.PhotoImage(img)
168
-
169
- app.test_canvas.create_image(500,120,anchor='nw', image=app.test_canvas.photo)
170
-
171
-
172
-
173
- app.test_canvas2.photo = ImageTk.PhotoImage(img)
174
-
175
- app.test_canvas3.photo = ImageTk.PhotoImage(img)
176
-
177
- app.test_canvas4.photo = ImageTk.PhotoImage(img)
178
-
179
-
180
-
181
-
182
-
183
- def set_image4(img1,img2,img3,img4):
184
-
185
- app.test_canvas.photo = ImageTk.PhotoImage(img1)
186
-
187
- app.test_canvas.itemconfig(app.image_on_canvas, image=app.test_canvas.photo)
188
-
189
- app.test_canvas2.photo = ImageTk.PhotoImage(img2)
190
-
191
- app.test_canvas.itemconfig(app.image_on_canvas2, image=app.test_canvas2.photo)
192
-
193
- app.test_canvas3.photo = ImageTk.PhotoImage(img3)
194
-
195
- app.test_canvas.itemconfig(app.image_on_canvas3, image=app.test_canvas3.photo)
196
-
197
- app.test_canvas4.photo = ImageTk.PhotoImage(img4)
198
-
199
- app.test_canvas.itemconfig(app.image_on_canvas4, image=app.test_canvas4.photo)
200
-
201
-
202
-
203
-
204
-
205
- def change_graphics(event):
206
-
207
- app.count+=1
208
-
209
- R=random.randint(1,260)
210
-
211
- R2=random.randint(1,260)
212
-
213
- R3=random.randint(1,260)
214
-
215
- R4=random.randint(1,260)
216
-
217
- original_image= Image.open(open('sample'+str(R)+'.png', 'rb'))
218
-
219
- original_image2= Image.open(open('sample'+str(R2)+'.png', 'rb'))
220
-
221
- original_image3= Image.open(open('sample'+str(R3)+'.png', 'rb'))
222
-
223
- original_image4= Image.open(open('sample'+str(R4)+'.png', 'rb'))
224
-
225
- if(app.count%2==0):
226
-
227
- set_image4(original_image,original_image2,original_image3,original_image4)
228
-
229
- else:
230
-
231
- set_image(original_image)
232
-
233
-
234
-
235
-
236
-
237
- # 画像ファイル読み込み
238
-
239
- R=random.randint(1,260)
240
-
241
- R2=random.randint(1,260)
242
-
243
- R3=random.randint(1,260)
244
-
245
- R4=random.randint(1,260)
246
-
247
- original_image = Image.open('black.jpg')
248
-
249
- original_image2 = Image.open('black.jpg')
250
-
251
- original_image3 = Image.open('black.jpg')
252
-
253
- original_image4 = Image.open('black.jpg')
254
-
255
-
256
-
257
- # アプリケーション起動
258
-
259
- root = tkinter.Tk()
260
-
261
- app = Application(master=root)
262
-
263
- app.mainloop()
264
-
265
-
266
-
267
- ```
268
-
269
265
 
270
266
 
271
267
  ### 補足情報