【发布时间】:2019-10-18 05:56:40
【问题描述】:
我尝试在 tkinter 中为嵌入式 matplotlib 绘图添加一些选项。绘图样式(即实线或虚线)正确更改,但颜色没有。但是,通过调整 tkinter 窗口的大小,颜色得到了更新。我的代码的相关部分如下所示:
import tkinter as tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
from tkinter.colorchooser import askcolor
import numpy as np
class Ploter:
DEFAULT_PEN_SIZE = 5.0
DEFAULT_COLOR = 'black'
DEFAULT_STYLE = '-'
def __init__(self, master, size = (5,4), dpi = 100):
self.master = master
self.size = size
self.dpi = dpi
self.color = self.DEFAULT_COLOR
self.style = self.DEFAULT_STYLE
self.fig = Figure(figsize = self.size, dpi=self.dpi)
self.canvas = FigureCanvasTkAgg(self.fig , master=self.master)
self.ax = self.fig.add_subplot(111)
self.t = np.arange(0, 3, 0.01)
self.tf = 2 * np.sin(2 * np.pi * self.t)
def PlotWidgets(self):
toolbar = NavigationToolbar2TkAgg(self.canvas, self.master)
toolbar.update()
self.canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
list1 = ["Solid", "Dashed", "Dash-dot", 'Dotted']
self.line_style = tk.StringVar()
self.line_style.set('Line Style')
self.line_style.trace("w", self.choose_style)
droplist = tk.OptionMenu(self.master, self.line_style, *list1)
droplist.config(width=10)
droplist.pack(side = tk.LEFT , padx = 10 , pady = 10)
color_button = tk.Button(master = self.master, text='color',
command = self.choose_color)
color_button.pack(side = tk.LEFT , padx = 10 , pady = 10)
self.ploter()
def ploter(self):
self.ax.clear()
style = self.style
self.ax.plot(self.t, self.tf, style , color = self.color)
self.canvas.draw()
def choose_color(self):
style = self.style
self.color = askcolor(color=self.color)[1]
self.ax.plot(self.t, self.tf, style , color = self.color)
#self.canvas.draw()
def choose_style(self, * args):
if self.line_style.get() == "Solid":
self.style = '-'
self.ploter()
if self.line_style.get() == "Dashed":
self.style = '--'
self.ploter()
if self.line_style.get() == "Dash-dot":
self.style = '-.'
self.ploter()
if self.line_style.get() == "Dotted":
self.style = ':'
self.ploter()
root = tk.Tk()
root.wm_title("Embedding in tkinter")
pp = Ploter(root)
pp.PlotWidgets()
root.mainloop()
我尝试过重启电脑、不同的 IDE 或从终端运行脚本,但都没有解决问题。
值得一提的是,我使用的是 Linux mint 19.2(MATE 桌面,x11 显示服务器),python 3.6.8,matplotlib 2.1.1,tkinter 8.6。
【问题讨论】:
标签: python matplotlib tkinter