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

質問編集履歴

1

変更後のコードと画像

2020/11/10 11:14

投稿

power-chan
power-chan

スコア23

title CHANGED
File without changes
body CHANGED
@@ -11,9 +11,116 @@
11
11
  ### 該当のソースコード
12
12
 
13
13
  ```python
14
- #先に表示させるウィンドウ
15
14
  import tkinter
15
+ def create_window():
16
+ import random
17
+ D = random.randint(0,5)
18
+ #ウィンドウ作成
19
+ root = tkinter.Tk()
20
+ root.title("運勢占い")
21
+ root.minsize(640, 480)
22
+ root.option_add("*font", ["MS Pゴシック", 22])
23
+ #画像読み込み
24
+ getu = tkinter.PhotoImage(file="大吉.png")
25
+ ka = tkinter.PhotoImage(file="凶.png")
26
+ sui = tkinter.PhotoImage(file="吉.png")
27
+ moku = tkinter.PhotoImage(file="小吉.png")
28
+ #画像表示
29
+ canvas = tkinter.Canvas(bg="plum", width=640, height=480)
30
+ canvas.place(x=0, y=0)
31
+ img = tkinter.PhotoImage(file="神社.png")
32
+ canvas.create_image(320, 240, image=img)
33
+ #ラベル01
34
+ question1 = tkinter.Label(text="あなたが入力した数字で今日の運勢を占います。", bg="plum")
35
+ question1.place(x=20, y=23)
36
+ question2 = tkinter.Label(text="を↓に入力してください。", bg="plum")
37
+ question2.place(x=279, y=60)
38
+ question3 = tkinter.Label(text="0~10の数字",fg="red", bg="plum")
39
+ question3.place(x=117, y=60)
40
+ #入力ボックス
41
+ entry = tkinter.Entry(width=2, bd=4, bg="#f9f948")
42
+ entry.place(x=300, y=140)
16
43
 
44
+ #決定ボタン
45
+ button = tkinter.Button(text="占うボタン",bd=3, bg="#e0a3ff")
46
+ button.place(x=240, y=400)
47
+ #おみくじ始まり
48
+ def btn_click():
49
+ a = float(entry.get())
50
+ if a > 10:
51
+ again_text = tkinter.Label(text="0から10の数字を入力してください。",fg="red",bg="plum")
52
+ again_text.place(x=105,y=30)
53
+ question1.destroy()
54
+ question2.destroy()
55
+ question3.destroy()
56
+ return
57
+ if a < 0:
58
+ again_text = tkinter.Label(text="0から10の数字を入力してください。",fg="red",bg="plum")
59
+ again_text.place(x=105,y=30)
60
+ question1.destroy()
61
+ question2.destroy()
62
+ question3.destroy()
63
+ return
64
+ if a+D == 8 or a+D == 9:
65
+ again_text = tkinter.Label(text="                        ",fg="red",bg="plum")
66
+ again_text.place(x=100,y=30)
67
+ res_text = tkinter.Label(text="あなたの今日の運勢は大吉です。",fg="red", bg="plum")
68
+ res_text.place(x=115, y=20)
69
+ res_text = tkinter.Label(text="やったね!", bg="plum")
70
+ res_text.place(x=250, y=425)
71
+ canvas.delete("all")
72
+ question1.destroy()
73
+ question2.destroy()
74
+ question3.destroy()
75
+ entry.destroy()
76
+ button.destroy()
77
+ canvas.create_image(320, 240, image=getu)
78
+ if 10 <= a+D <= 15:
79
+ again_text = tkinter.Label(text="                           ",fg="red",bg="plum")
80
+ again_text.place(x=100,y=30)
81
+ res_text = tkinter.Label(text="あなたの今日の運勢は凶です。",fg="darkviolet",bg="plum")
82
+ res_text.place(x=120, y=20)
83
+ res_text = tkinter.Label(text="ざんねーん", bg="plum")
84
+ res_text.place(x=250, y=425)
85
+ canvas.delete("all")
86
+ question1.destroy()
87
+ question2.destroy()
88
+ question3.destroy()
89
+ entry.destroy()
90
+ button.destroy()
91
+ canvas.create_image(320, 240, image=ka)
92
+ if 4 <= a+D <= 7:
93
+ again_text = tkinter.Label(text="                           ",fg="red",bg="plum")
94
+ again_text.place(x=100,y=30)
95
+ res_text = tkinter.Label(text="あなたの今日の運勢は吉です。",fg="green",bg="plum")
96
+ res_text.place(x=120, y=24)
97
+ res_text = tkinter.Label(text="おっええやん", bg="plum")
98
+ res_text.place(x=240, y=430)
99
+ canvas.delete("all")
100
+ question1.destroy()
101
+ question2.destroy()
102
+ question3.destroy()
103
+ entry.destroy()
104
+ button.destroy()
105
+ canvas.create_image(320, 245, image=sui)
106
+ if 0 <= a+D <= 3:
107
+ again_text = tkinter.Label(text="                           ",fg="red",bg="plum")
108
+ again_text.place(x=100,y=30)
109
+ res_text = tkinter.Label(text="あなたの今日の運勢は小吉です。",fg="sienna",bg="plum")
110
+ res_text.place(x=120, y=20)
111
+ res_text = tkinter.Label(text="いいじゃん!", bg="plum")
112
+ res_text.place(x=243, y=425)
113
+ canvas.delete("all")
114
+ question1.destroy()
115
+ question2.destroy()
116
+ question3.destroy()
117
+ entry.destroy()
118
+ button.destroy()
119
+ canvas.create_image(320, 240, image=moku)
120
+
121
+ button["command"] = btn_click
122
+ root.mainloop()
123
+
17
124
  root = tkinter.Tk()
18
125
  root.title("運勢占い")
19
126
  root.minsize(640, 480)
@@ -28,124 +135,14 @@
28
135
  intro = tkinter.Label(text = "ここではあなたが入力した数字で\n今日の運勢を占います", bg="black", fg="red")
29
136
  intro.place(x=120, y=230)
30
137
 
31
- button = tkinter.Button(text = "次へ" ,bd = 3, bg= "black", fg="#3374ff")
138
+ button = tkinter.Button(text = "次へ" ,bd = 3, bg= "black", fg="#3374ff", command=create_window)
32
139
  button.place(x=280, y=370)
33
140
 
34
141
  root.mainloop()
35
142
 
36
-
37
-
38
- #ボタンが押された後に表示したいウィンドウ
39
- import tkinter
40
- #数字をランダム出力
41
- import random
42
- D = random.randint(0,5)
43
- #ウィンドウ作成
44
- root = tkinter.Tk()
45
- root.title("運勢占い")
46
- root.minsize(640, 480)
47
- root.option_add("*font", ["MS Pゴシック", 22])
48
- #画像読み込み
49
- getu = tkinter.PhotoImage(file="大吉.png")
50
- ka = tkinter.PhotoImage(file="凶.png")
51
- sui = tkinter.PhotoImage(file="吉.png")
52
- moku = tkinter.PhotoImage(file="小吉.png")
53
- #画像表示
54
- canvas = tkinter.Canvas(bg="plum", width=640, height=480)
55
- canvas.place(x=0, y=0)
56
- img = tkinter.PhotoImage(file="神社.png")
57
- canvas.create_image(320, 240, image=img)
58
- #ラベル01
59
- question1 = tkinter.Label(text="あなたが入力した数字で今日の運勢を占います。", bg="plum")
60
- question1.place(x=20, y=23)
61
- question2 = tkinter.Label(text="を↓に入力してください。", bg="plum")
62
- question2.place(x=279, y=60)
63
- question3 = tkinter.Label(text="0~10の数字",fg="red", bg="plum")
64
- question3.place(x=117, y=60)
65
- #入力ボックス
66
- entry = tkinter.Entry(width=2, bd=4, bg="#f9f948")
67
- entry.place(x=300, y=140)
68
-
69
- #決定ボタン
70
- button = tkinter.Button(text="占うボタン",bd=3, bg="#e0a3ff")
71
- button.place(x=240, y=400)
72
- #おみくじ始まり
73
- def btn_click():
74
- a = float(entry.get())
75
- if a > 10:
76
- again_text = tkinter.Label(text="0から10の数字を入力してください。",fg="red",bg="plum")
77
- again_text.place(x=105,y=30)
78
- question1.destroy()
79
- question2.destroy()
80
- question3.destroy()
81
- return
82
- if a < 0:
83
- again_text = tkinter.Label(text="0から10の数字を入力してください。",fg="red",bg="plum")
84
- again_text.place(x=105,y=30)
85
- question1.destroy()
86
- question2.destroy()
87
- question3.destroy()
88
- return
89
- if a+D == 8 or a+D == 9:
90
- again_text = tkinter.Label(text="                        ",fg="red",bg="plum")
91
- again_text.place(x=100,y=30)
92
- res_text = tkinter.Label(text="あなたの今日の運勢は大吉です。",fg="red", bg="plum")
93
- res_text.place(x=115, y=20)
94
- res_text = tkinter.Label(text="やったね!", bg="plum")
95
- res_text.place(x=250, y=425)
96
- canvas.delete("all")
97
- question1.destroy()
98
- question2.destroy()
99
- question3.destroy()
100
- entry.destroy()
101
- button.destroy()
102
- canvas.create_image(320, 240, image=getu)
103
- if 10 <= a+D <= 15:
104
- again_text = tkinter.Label(text="                           ",fg="red",bg="plum")
105
- again_text.place(x=100,y=30)
106
- res_text = tkinter.Label(text="あなたの今日の運勢は凶です。",fg="darkviolet",bg="plum")
107
- res_text.place(x=120, y=20)
108
- res_text = tkinter.Label(text="ざんねーん", bg="plum")
109
- res_text.place(x=250, y=425)
110
- canvas.delete("all")
111
- question1.destroy()
112
- question2.destroy()
113
- question3.destroy()
114
- entry.destroy()
115
- button.destroy()
116
- canvas.create_image(320, 240, image=ka)
117
- if 4 <= a+D <= 7:
118
- again_text = tkinter.Label(text="                           ",fg="red",bg="plum")
119
- again_text.place(x=100,y=30)
120
- res_text = tkinter.Label(text="あなたの今日の運勢は吉です。",fg="green",bg="plum")
121
- res_text.place(x=120, y=24)
122
- res_text = tkinter.Label(text="おっええやん", bg="plum")
123
- res_text.place(x=240, y=430)
124
- canvas.delete("all")
125
- question1.destroy()
126
- question2.destroy()
127
- question3.destroy()
128
- entry.destroy()
129
- button.destroy()
130
- canvas.create_image(320, 245, image=sui)
131
- if 0 <= a+D <= 3:
132
- again_text = tkinter.Label(text="                           ",fg="red",bg="plum")
133
- again_text.place(x=100,y=30)
134
- res_text = tkinter.Label(text="あなたの今日の運勢は小吉です。",fg="sienna",bg="plum")
135
- res_text.place(x=120, y=20)
136
- res_text = tkinter.Label(text="いいじゃん!", bg="plum")
137
- res_text.place(x=243, y=425)
138
- canvas.delete("all")
139
- question1.destroy()
140
- question2.destroy()
141
- question3.destroy()
142
- entry.destroy()
143
- button.destroy()
144
- canvas.create_image(320, 240, image=moku)
145
-
146
- button["command"] = btn_click
147
- root.mainloop()
148
143
  ```
144
+ ###追記
145
+ ![イメージ説明](bd9b04e3a83c6d793a15ba8f343e4ca9.png)
149
146
 
150
147
  ### 試したこと
151
148
  button = tkinter.Button(text = "次へ" ,bd = 3, bg= "black", fg="#3374ff")