質問編集履歴

1

現在作成しているプログラムを載せました。

2020/09/01 05:48

投稿

dddddod
dddddod

スコア4

test CHANGED
File without changes
test CHANGED
@@ -1 +1,247 @@
1
- jupyterのpythonでボタン式の関数電卓を作っているのですがその機能に変数x,yを追加して関数グラフを描写するプログラムを追加したいのですがどのように追加したらよいでしょうか?
1
+ jupyterのpythonでボタン式の関数電卓を作っているのですがその機能に変数x,yと関数用の=を追加して関数グラフを描写するプログラムを下記のプログラムに追加したいのですがどのように追加したらよいでしょうか?
2
+
3
+ ```ここに言語を入力
4
+
5
+ コード python
6
+
7
+ import math
8
+
9
+ import tkinter as tk
10
+
11
+
12
+
13
+
14
+
15
+
16
+
17
+ class Application(tk.Frame):
18
+
19
+
20
+
21
+ def __init__(self, master=None):
22
+
23
+ super().__init__(master)
24
+
25
+ self.master.geometry('500x700')
26
+
27
+
28
+
29
+ self.master.title('計算機')
30
+
31
+
32
+
33
+ self.entry = tk.Entry(self.master, justify="right")
34
+
35
+
36
+
37
+ self.menu_bar = tk.Menu(self.master)
38
+
39
+ self.master.config(menu=self.menu_bar)
40
+
41
+
42
+
43
+ self.create_widgets()
44
+
45
+
46
+
47
+
48
+
49
+ def input(self, action):
50
+
51
+ self.entry.insert(tk.END, action)
52
+
53
+
54
+
55
+ def clear_all(self):
56
+
57
+ self.entry.delete(0, tk.END)
58
+
59
+
60
+
61
+ def clear_one(self):
62
+
63
+ txt = self.entry.get()
64
+
65
+ self.entry.delete(0, tk.END)
66
+
67
+ self.entry.insert(0, txt[:-1])
68
+
69
+
70
+
71
+ def entered_value(self):
72
+
73
+ return eval(self.entry.get().replace('÷', '/').replace('x', '*').replace('%', '/100').replace('^','**').replace('√','**0.5').replace('e','math.e'))
74
+
75
+
76
+
77
+ def equals(self):
78
+
79
+ self.value = self.entered_value()
80
+
81
+ self.entry.delete(0, tk.END)
82
+
83
+ self.entry.insert(0, self.value)
84
+
85
+
86
+
87
+ def unary(self, function):
88
+
89
+ self.value = function(self.entered_value())
90
+
91
+ self.entry.delete(0, tk.END)
92
+
93
+ self.entry.insert(0, self.value)
94
+
95
+
96
+
97
+ def create_widgets(self):
98
+
99
+ file_menu = tk.Menu(self.menu_bar)
100
+
101
+ file_menu.add_command(label='閉じる', command=self.master.quit)
102
+
103
+ self.menu_bar.add_cascade(label='メニュー', menu=file_menu)
104
+
105
+
106
+
107
+ self.entry.grid(row=0, column=0, columnspan=4, pady=3)
108
+
109
+ self.entry.focus_set()
110
+
111
+
112
+
113
+ tk.Button(self.master, text='7', width=7,
114
+
115
+ command=lambda: self.input(7)).grid(row=2, column=0)
116
+
117
+ tk.Button(self.master, text='8', width=7,
118
+
119
+ command=lambda: self.input(8)).grid(row=2, column=1)
120
+
121
+ tk.Button(self.master, text='9', width=7,
122
+
123
+ command=lambda: self.input(9)).grid(row=2, column=2)
124
+
125
+
126
+
127
+ tk.Button(self.master, text='4', width=7,
128
+
129
+ command=lambda: self.input(4)).grid(row=3, column=0)
130
+
131
+ tk.Button(self.master, text='5', width=7,
132
+
133
+ command=lambda: self.input(5)).grid(row=3, column=1)
134
+
135
+ tk.Button(self.master, text='6', width=7,
136
+
137
+ command=lambda: self.input(6)).grid(row=3, column=2)
138
+
139
+
140
+
141
+ tk.Button(self.master, text='1', width=7,
142
+
143
+ command=lambda: self.input(1)).grid(row=4, column=0)
144
+
145
+ tk.Button(self.master, text='2', width=7,
146
+
147
+ command=lambda: self.input(2)).grid(row=4, column=1)
148
+
149
+ tk.Button(self.master, text='3', width=7,
150
+
151
+ command=lambda: self.input(3)).grid(row=4, column=2)
152
+
153
+
154
+
155
+ tk.Button(self.master, text='0', width=12,
156
+
157
+ command=lambda: self.input(0)).grid(row=5, column=0, columnspan=2)
158
+
159
+ tk.Button(self.master, text='.', width=7,
160
+
161
+ command=lambda: self.input('.')).grid(row=5, column=2)
162
+
163
+ tk.Button(self.master, text='=', width=7,
164
+
165
+ command=self.equals).grid(row=5, column=3)
166
+
167
+
168
+
169
+ tk.Button(self.master, text='x', width=7,
170
+
171
+ command=lambda: self.input('x')).grid(row=2, column=3)
172
+
173
+ tk.Button(self.master, text='-', width=7,
174
+
175
+ command=lambda: self.input('-')).grid(row=3, column=3)
176
+
177
+ tk.Button(self.master, text='+', width=7,
178
+
179
+ command=lambda: self.input('+')).grid(row=4, column=3)
180
+
181
+
182
+
183
+ tk.Button(self.master, text='AC', width=7,
184
+
185
+ command=lambda: self.clear_all()).grid(row=1, column=0)
186
+
187
+ tk.Button(self.master, text='C', width=7,
188
+
189
+ command=lambda: self.clear_one()).grid(row=1, column=1)
190
+
191
+ tk.Button(self.master, text='%', width=7,
192
+
193
+ command=lambda: self.input('%')).grid(row=1, column=2)
194
+
195
+ tk.Button(self.master, text='÷', width=7,
196
+
197
+ command=lambda: self.input('÷')).grid(row=1, column=3)
198
+
199
+
200
+
201
+ tk.Button(self.master, text='sin', width=7,
202
+
203
+ command=lambda: self.unary(math.sin)).grid(row=1, column=5)
204
+
205
+ tk.Button(self.master, text='cos', width=7,
206
+
207
+ command=lambda: self.unary(math.cos)).grid(row=2, column=5)
208
+
209
+ tk.Button(self.master, text='tan', width=7,
210
+
211
+ command=lambda: self.unary(math.tan)).grid(row=3, column=5)
212
+
213
+ tk.Button(self.master, text='log', width=7,
214
+
215
+ command=lambda: self.unary(math.log10)).grid(row=4, column=5)
216
+
217
+
218
+
219
+ tk.Button(self.master, text='^', width=7,
220
+
221
+ command=lambda: self.input('^')).grid(row=1,column=4)
222
+
223
+ tk.Button(self.master, text='π', width=7,
224
+
225
+ command=lambda: self.input(math.pi)).grid(row=2,column=4)
226
+
227
+ tk.Button(self.master, text='√', width=7,
228
+
229
+ command=lambda: self.input('√')).grid(row=3,column=4)
230
+
231
+ tk.Button(self.master, text='e', width=7,
232
+
233
+ command=lambda: self.input('e')).grid(row=4,column=4)
234
+
235
+ tk.Button(self.master, text='!', width=7,
236
+
237
+ command=lambda: self.unary(math.factorial)).grid(row=5,column=4)
238
+
239
+
240
+
241
+ root = tk.Tk()
242
+
243
+ app = Application(master=root)
244
+
245
+ app.mainloop()
246
+
247
+ ```