pythonで関数電卓のプログラムを作っているのですが演算子以外の計算(対数、三角関数、etc…)をどう書けば良いのかが分からないので教えていただきたいです。
python
1コード 2 3import tkinter as tk 4 5 6class Application(tk.Frame): 7 8 def __init__(self, master=None): 9 super().__init__(master) 10 self.master.geometry() 11 self.master.title('計算機') 12 13 self.entry = tk.Entry(self.master, justify="right") 14 15 self.menu_bar = tk.Menu(self.master) 16 self.master.config(menu=self.menu_bar) 17 18 self.create_widgets() 19 20 def input(self, action): 21 self.entry.insert(tk.END, action) 22 23 def clear_all(self): 24 self.entry.delete(0, tk.END) 25 26 def clear_one(self): 27 txt = self.entry.get() 28 self.entry.delete(0, tk.END) 29 self.entry.insert(0, txt[:-1]) 30 31 def equals(self): 32 self.value = eval(self.entry.get().replace('÷', '/').replace('x', '*').replace('%', '/100')) 33 self.entry.delete(0, tk.END) 34 self.entry.insert(0, self.value) 35 36 def create_widgets(self): 37 file_menu = tk.Menu(self.menu_bar) 38 file_menu.add_command(label='閉じる', command=self.master.quit) 39 self.menu_bar.add_cascade(label='メニュー', menu=file_menu) 40 41 self.entry.grid(row=0, column=0, columnspan=4, pady=3) 42 self.entry.focus_set() 43 44 tk.Button(self.master, text='7', width=4, 45 command=lambda: self.input(7)).grid(row=2, column=0) 46 tk.Button(self.master, text='8', width=4, 47 command=lambda: self.input(8)).grid(row=2, column=1) 48 tk.Button(self.master, text='9', width=4, 49 command=lambda: self.input(9)).grid(row=2, column=2) 50 51 tk.Button(self.master, text='4', width=4, 52 command=lambda: self.input(4)).grid(row=3, column=0) 53 tk.Button(self.master, text='5', width=4, 54 command=lambda: self.input(5)).grid(row=3, column=1) 55 tk.Button(self.master, text='6', width=4, 56 command=lambda: self.input(6)).grid(row=3, column=2) 57 58 tk.Button(self.master, text='1', width=4, 59 command=lambda: self.input(1)).grid(row=4, column=0) 60 tk.Button(self.master, text='2', width=4, 61 command=lambda: self.input(2)).grid(row=4, column=1) 62 tk.Button(self.master, text='3', width=4, 63 command=lambda: self.input(3)).grid(row=4, column=2) 64 65 tk.Button(self.master, text='0', width=9, 66 command=lambda: self.input(0)).grid(row=5, column=0, columnspan=2) 67 tk.Button(self.master, text='.', width=4, 68 command=lambda: self.input('.')).grid(row=5, column=2) 69 tk.Button(self.master, text='=', width=4, 70 command=self.equals).grid(row=5, column=3) 71 72 tk.Button(self.master, text='x', width=4, 73 command=lambda: self.input('x')).grid(row=2, column=3) 74 tk.Button(self.master, text='-', width=4, 75 command=lambda: self.input('-')).grid(row=3, column=3) 76 tk.Button(self.master, text='+', width=4, 77 command=lambda: self.input('+')).grid(row=4, column=3) 78 79 tk.Button(self.master, text='AC', width=4, 80 command=lambda: self.clear_all()).grid(row=1, column=0) 81 tk.Button(self.master, text='C', width=4, 82 command=lambda: self.clear_one()).grid(row=1, column=1) 83 tk.Button(self.master, text='%', width=4, 84 command=lambda: self.input('%')).grid(row=1, column=2) 85 tk.Button(self.master, text='÷', width=4, 86 command=lambda: self.input('÷')).grid(row=1, column=3) 87 88 89root = tk.Tk() 90app = Application(master=root) 91app.mainloop() 92
回答1件
あなたの回答
tips
プレビュー