How can I click on the GUI button to display the graph that I have coded?
from tkinter import *
root = Tk()
frame = Frame(root)
frame.pack()
bottomframe = Frame(root)
bottomframe.pack(side=BOTTOM)
redbutton = Button(frame, text='Training 1', fg='red')
redbutton.pack(side=LEFT)
greenbutton = Button(frame, text='Training 2', fg='brown')
greenbutton.pack(side=LEFT)
bluebutton = Button(frame, text='Testing 1', fg='blue')
bluebutton.pack(side=LEFT)
blackbutton = Button(bottomframe, text='Testing 2', fg='black')
blackbutton.pack(side=BOTTOM)
root.mainloop()
Based on the code above, there are 4 buttons display in the graphical user interface. I would like to press on the Training 1
button to display my graph below.
""" Plot """
plt.plot(scores_train, 'b')
plt.title('Model Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend(['train'], loc='upper left')
plt.show()
CodePudding user response:
You need to import pyplot from matplotlib and then define the function for plotting the graph. Suppose we call the function 'graph', it would look like this:
def graph():
plt.plot(scores_train, 'b')
plt.title('Model Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend(['train'], loc='upper left')
plt.show()
Then you need to add the command for the redbutton, like this:
redbutton = Button(frame, text='Training 1', command=graph, fg='red')
Hope this works for you!