jupyterのpythonでtkinterとmatplotlibを使用して一次、二次関数を電卓のプログラムを駆使してグラフを描写したいのですがどうしたらよいでしょうか。ボタンで入力で計算は
行えるのですがグラフが出力できません。
python
1コード 2import math 3import tkinter as tk 4import matplotlib.pyplot as plt 5%matplotlib inline 6 7 8class Application(tk.Frame): 9 10 def __init__(self, master=None): 11 super().__init__(master) 12 self.master.geometry('500x700') 13 14 self.master.title('計算機') 15 16 self.entry = tk.Entry(self.master, justify="right") 17 18 self.menu_bar = tk.Menu(self.master) 19 self.master.config(menu=self.menu_bar) 20 21 self.create_widgets() 22 23 24 def input(self, action): 25 self.entry.insert(tk.END, action) 26 27 def clear_all(self): 28 self.entry.delete(0, tk.END) 29 30 def clear_one(self): 31 txt = self.entry.get() 32 self.entry.delete(0, tk.END) 33 self.entry.insert(0, txt[:-1]) 34 35 def entered_value(self): 36 return eval(self.entry.get().replace('÷', '/').replace('x', '*').replace('%', '/100').replace('^','**').replace('√','**0.5').replace('e','math.e')) 37 38 def equals(self): 39 self.value = self.entered_value() 40 self.entry.delete(0, tk.END) 41 self.entry.insert(0, self.value) 42 43 def unary(self, function): 44 self.value = function(self.entered_value()) 45 self.entry.delete(0, tk.END) 46 self.entry.insert(0, self.value) 47 48 def create_widgets(self): 49 file_menu = tk.Menu(self.menu_bar) 50 file_menu.add_command(label='閉じる', command=self.master.quit) 51 self.menu_bar.add_cascade(label='メニュー', menu=file_menu) 52 53 self.entry.grid(row=0, column=0, columnspan=4, pady=3) 54 self.entry.focus_set() 55 56 tk.Button(self.master, text='7', width=7, 57 command=lambda: self.input(7)).grid(row=2, column=0) 58 tk.Button(self.master, text='8', width=7, 59 command=lambda: self.input(8)).grid(row=2, column=1) 60 tk.Button(self.master, text='9', width=7, 61 command=lambda: self.input(9)).grid(row=2, column=2) 62 63 tk.Button(self.master, text='4', width=7, 64 command=lambda: self.input(4)).grid(row=3, column=0) 65 tk.Button(self.master, text='5', width=7, 66 command=lambda: self.input(5)).grid(row=3, column=1) 67 tk.Button(self.master, text='6', width=7, 68 command=lambda: self.input(6)).grid(row=3, column=2) 69 70 tk.Button(self.master, text='1', width=7, 71 command=lambda: self.input(1)).grid(row=4, column=0) 72 tk.Button(self.master, text='2', width=7, 73 command=lambda: self.input(2)).grid(row=4, column=1) 74 tk.Button(self.master, text='3', width=7, 75 command=lambda: self.input(3)).grid(row=4, column=2) 76 77 tk.Button(self.master, text='0', width=12, 78 command=lambda: self.input(0)).grid(row=5, column=0, columnspan=2) 79 tk.Button(self.master, text='.', width=7, 80 command=lambda: self.input('.')).grid(row=5, column=2) 81 tk.Button(self.master, text='=', width=7, 82 command=self.equals).grid(row=5, column=3) 83 84 tk.Button(self.master, text='x', width=7, 85 command=lambda: self.input('x')).grid(row=2, column=3) 86 tk.Button(self.master, text='-', width=7, 87 command=lambda: self.input('-')).grid(row=3, column=3) 88 tk.Button(self.master, text='+', width=7, 89 command=lambda: self.input('+')).grid(row=4, column=3) 90 91 tk.Button(self.master, text='AC', width=7, 92 command=lambda: self.clear_all()).grid(row=1, column=0) 93 tk.Button(self.master, text='C', width=7, 94 command=lambda: self.clear_one()).grid(row=1, column=1) 95 tk.Button(self.master, text='%', width=7, 96 command=lambda: self.input('%')).grid(row=1, column=2) 97 tk.Button(self.master, text='÷', width=7, 98 command=lambda: self.input('÷')).grid(row=1, column=3) 99 100 tk.Button(self.master, text='sin', width=7, 101 command=lambda: self.unary(math.sin)).grid(row=1, column=5) 102 tk.Button(self.master, text='cos', width=7, 103 command=lambda: self.unary(math.cos)).grid(row=2, column=5) 104 tk.Button(self.master, text='tan', width=7, 105 command=lambda: self.unary(math.tan)).grid(row=3, column=5) 106 tk.Button(self.master, text='log', width=7, 107 command=lambda: self.unary(math.log10)).grid(row=4, column=5) 108 109 tk.Button(self.master, text='^', width=7, 110 command=lambda: self.input('^')).grid(row=1,column=4) 111 tk.Button(self.master, text='π', width=7, 112 command=lambda: self.input(math.pi)).grid(row=2,column=4) 113 tk.Button(self.master, text='√', width=7, 114 command=lambda: self.input('√')).grid(row=3,column=4) 115 tk.Button(self.master, text='e', width=7, 116 command=lambda: self.input('e')).grid(row=4,column=4) 117 tk.Button(self.master, text='!', width=7, 118 command=lambda: self.unary(math.factorial)).grid(row=5,column=4) 119 120fig = plt.figure() 121X = fig.add_subplot(221) 122 123X.plot(dat) 124 125plt.show() 126 127 128 129 130root = tk.Tk() 131app = Application(master=root) 132app.mainloop()
こちらを実行すると終了間際にまっさらなグラフがでできます。
あなたの回答
tips
プレビュー