【发布时间】:2018-03-26 04:42:50
【问题描述】:
我正在开发这个应用程序,它涉及在按下按钮后在空白图上绘制一条线或圆。一切都很好,直到突然,图表停止更新。例如:
import math
import tkinter as tk
from tkinter import *
import matplotlib
import matplotlib.pyplot as plt
from numpy import arange, sin, cos, tan, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg,
NavigationToolbar2TkAgg
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
from matplotlib.patches import Circle
# Initialising the graph
f = Figure(figsize=(6, 6), dpi=100)
a = f.add_subplot(111)
root = tk.Tk()
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.pack()
self.mainwindow()
def mainwindow(self):
self.add_loci = Button(self)
self.add_loci["text"] = "Add Line"
self.add_loci["command"] = self.line
self.add_loci.pack(side = TOP)
# Plotting an empty graph
a.set_xlabel('Re')
a.set_ylabel('Im')
a.grid(b=None)
a.plot(0, 0)
# Setting up and showing the toolbar and the graph
canvas = FigureCanvasTkAgg(f, master=root)
canvas.show()
canvas.get_tk_widget().pack(side = BOTTOM, fill=BOTH, expand=1)
toolbar = NavigationToolbar2TkAgg(canvas, root)
toolbar.update()
canvas._tkcanvas.pack(side = BOTTOM, fill=BOTH, expand=1)
def line(self):
a.plot([0, 10], [0, 10])
print('test')
app = Application(master=root)
app.mainloop()
按下按钮时的假设结果是应该出现顺子,但事实并非如此。我添加了打印语句来检查按钮是否正常工作并且确实如此,所以我真的不知道这里的问题是什么。我目前正在使用 Spyder (3.2.4)。
【问题讨论】:
标签: python python-3.x matplotlib tkinter