前提・実現したいこと
エントリから係数を取得し,二次関数(y = ax^2+bx+c)のグラフ出力するプログラムです.
python3でtkinterでインターフェースを作成し,matplotlibを使ってグラフを作成しています.
初めてインターフェースを作成していますが,下の問題で苦戦しています.
発生している問題・エラーメッセージ
グラフが出力されず,困っています.
コードのどこに問題が有るのかを知りたいです.
ソースコードは下に全容を示しています.
よろしくお願いします.
該当のソースコード
python3
1import tkinter as tk 2import numpy as np 3from matplotlib import pyplot as plt 4 5 6#If you're in trouble, refer to "http://www.geocities.jp/m_hiroi/light/pytk01.html". 7 8# Create entries 9entry_w = 8 10entry_y = 30 11 12def MakeGraph(a_box, b_box, c_box, fig, canvas): 13 # Get the coefficients 14 coefficient_a = float(a_box.get()) 15 coefficient_b = float(b_box.get()) 16 coefficient_c = float(c_box.get()) 17 18 # Calcurate the quadratic equation 19 x_min, x_max = -50, 50 20 x = np.linspace(x_min, x_max) 21 y = coefficient_a * x ** 2 + coefficient_b * x + coefficient_c 22 23 ax.plot(x, y, linestyle = "--", color = "black", label = 'y = ax^2 + bx + c') 24 25 graph = plt.savefig("figure.png") 26 graph = tk.PhotoImage(file = "figure.png") 27 28 canvas.create_image(0,0, image = graph) 29 canvas.pack() 30 31 print("Making a graph is finished.") 32 33 return 34 35root = tk.Tk() 36root.title('Visualizer for quadratic curve') 37root.geometry('800x600') 38 39 40a_box = tk.Entry(width = entry_w) 41a_box.insert(tk.END,"1") 42a_box.pack() 43b_box = tk.Entry(width = entry_w) 44b_box.insert(tk.END,"2") 45b_box.pack() 46c_box = tk.Entry(width = entry_w) 47c_box.insert(tk.END,"1") 48c_box.pack() 49 50fig = plt.figure(figsize = (1,1)) 51ax = fig.add_subplot(111) 52 53canvas = tk.Canvas(root) 54 55# Create button 56button = tk.Button(root, text = 'Plot') 57button.bind("<Button-1>", MakeGraph(a_box, b_box, c_box, fig, canvas)) 58button.pack() 59 60 61root.mainloop()
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/02 02:57
2018/11/02 04:10