質問編集履歴

7

コードの追記

2022/03/22 05:52

投稿

nkfrom
nkfrom

スコア10

test CHANGED
File without changes
test CHANGED
@@ -50,10 +50,10 @@
50
50
  avg = rng_sum / qty
51
51
  la1 = tkinter.Label(root, text = "total number of times: {}".format(qty),
52
52
  fg = "white", bg = "green", font = fnt)
53
- la1.place(x = 0, y = 45)
53
+ la1.place(x = 0, y = 45) #今は config() で更新しています
54
54
  la2 = tkinter.Label(root, text = "average: {:.2f}".format(avg),
55
55
  fg = "white", bg = "green", font = fnt)
56
- la2.place(x = 0, y = 62)
56
+ la2.place(x = 0, y = 62) #同上
57
57
 
58
58
  def bu_on():
59
59
  thread1 = threading.Thread(target = work1)

6

追記

2022/03/19 07:34

投稿

nkfrom
nkfrom

スコア10

test CHANGED
File without changes
test CHANGED
@@ -113,7 +113,7 @@
113
113
  bu1.config(state = "disabled")
114
114
  else:
115
115
  bu1.config(state = "normal")
116
- #精神衛生上…
116
+ #なくてもいい?
117
117
  start_flag = False
118
118
 
119
119
  def start():

5

ソースコードの更新

2022/03/19 07:14

投稿

nkfrom
nkfrom

スコア10

test CHANGED
File without changes
test CHANGED
@@ -102,28 +102,60 @@
102
102
  root = tkinter.Tk()
103
103
  photo = tkinter.PhotoImage(file = "1~6/1.gif")
104
104
  gif_index = 0
105
+ start_flag = False
106
+
107
+ def times(t):
108
+ global start_flag
109
+ i = int(en.get() or 1)
110
+ if start_flag and t <= i:
111
+ roll()
112
+ root.after(1200, times, t + 1)
113
+ bu1.config(state = "disabled")
114
+ else:
115
+ bu1.config(state = "normal")
116
+ #精神衛生上…
117
+ start_flag = False
118
+
119
+ def start():
120
+ global start_flag
121
+ start_flag = True
122
+ times(1)
123
+
124
+ def stop():
125
+ global start_flag
126
+ start_flag = False
105
127
 
106
128
  def roll():
107
129
  global photo
108
130
  rng = random.randint(1, 6)
109
131
  photo = tkinter.PhotoImage(file="1~6/{}.gif".format(rng))
110
- label["image"] = photo
132
+ #la["image"] = photo
133
+ la.config(image = photo)
111
134
  gif()
112
135
 
113
136
  def gif():
114
137
  global gif_index
115
138
  try:
116
- photo.configure(format="gif -index {}".format(gif_index))
139
+ photo.config(format="gif -index {}".format(gif_index))
117
140
  gif_index += 1
118
141
  except tkinter.TclError:
119
142
  gif_index = 0
120
143
  else:
121
144
  root.after(10, gif)
122
145
 
146
+ #print(gif_index, end = " ")
147
+ #if gif_index == 0:
148
+ # print("\n")
149
+
123
- label = tkinter.Label(root, image = photo)
150
+ la = tkinter.Label(root, image = photo)
124
- label.pack()
151
+ en = tkinter.Entry()
125
- bu = tkinter.Button(root, text = "Roll", command = roll)
152
+ bu1 = tkinter.Button(root, text = "Roll", command = start)
153
+ bu2 = tkinter.Button(root, text = "Stop", command = stop)
154
+
155
+ la.pack()
156
+ en.pack()
126
- bu.pack()
157
+ bu1.pack()
158
+ bu2.pack()
127
159
 
128
160
  root.mainloop()
129
161
  ```

4

言語名の追加

2022/03/17 08:42

投稿

nkfrom
nkfrom

スコア10

test CHANGED
File without changes
test CHANGED
@@ -95,7 +95,7 @@
95
95
  初めてのアプリ作成なので見にくい部分もあると思いますが、よろしくお願いします。
96
96
 
97
97
  ### 追記
98
- ```
98
+ ```Python
99
99
  import tkinter
100
100
  import random
101
101
 

3

追記

2022/03/17 08:34

投稿

nkfrom
nkfrom

スコア10

test CHANGED
File without changes
test CHANGED
@@ -94,3 +94,37 @@
94
94
  Python 3.10.2
95
95
  初めてのアプリ作成なので見にくい部分もあると思いますが、よろしくお願いします。
96
96
 
97
+ ### 追記
98
+ ```
99
+ import tkinter
100
+ import random
101
+
102
+ root = tkinter.Tk()
103
+ photo = tkinter.PhotoImage(file = "1~6/1.gif")
104
+ gif_index = 0
105
+
106
+ def roll():
107
+ global photo
108
+ rng = random.randint(1, 6)
109
+ photo = tkinter.PhotoImage(file="1~6/{}.gif".format(rng))
110
+ label["image"] = photo
111
+ gif()
112
+
113
+ def gif():
114
+ global gif_index
115
+ try:
116
+ photo.configure(format="gif -index {}".format(gif_index))
117
+ gif_index += 1
118
+ except tkinter.TclError:
119
+ gif_index = 0
120
+ else:
121
+ root.after(10, gif)
122
+
123
+ label = tkinter.Label(root, image = photo)
124
+ label.pack()
125
+ bu = tkinter.Button(root, text = "Roll", command = roll)
126
+ bu.pack()
127
+
128
+ root.mainloop()
129
+ ```
130
+

2

追記

2022/03/10 05:50

投稿

nkfrom
nkfrom

スコア10

test CHANGED
File without changes
test CHANGED
@@ -86,6 +86,9 @@
86
86
 
87
87
  threadingにする前はウィンドウが閉じることはありませんでした。buのcommandをwork1にすると同じ状況になります。
88
88
 
89
+ ※追記
90
+ 同じ状況ではありませんでした。buのcommand、またはen.bindのlambdaをwork1にし、work1を1度でも実行すると、その後はbu_onを実行してもウィンドウが閉じることはありませんでした。根本的な解決には至っていないため、引き続き回答よろしくお願いします。
91
+
89
92
  ### 補足情報(FW/ツールのバージョンなど)
90
93
 
91
94
  Python 3.10.2

1

初心者マークの付け忘れ

2022/03/10 02:30

投稿

nkfrom
nkfrom

スコア10

test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,6 @@
1
1
 
2
2
 
3
- ### 前提・実現したいこと
3
+ ### 前提・実現したいこと
4
4
 
5
5
  Tkinterでサイコロを振るアプリを作成しています。
6
6
  ボタンを押すと乱数を生成し、それに対応したGIFが流れるというものです。