ItGo.me Focus on IT Recommend

Home > python - Matplotlib figures not moving with frames

python - Matplotlib figures not moving with frames

2020腾讯云双十一活动,全年最低!!!(领取3500元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1074

【阿里云】双十一活动,全年抄底价,限时3天!(老用户也有),
入口地址https://www.aliyun.com/1111/home

up vote 0 down vote favorite I can post the whole GUI and files necessary to run it, if needed. I'm trying to embed math equations in frames that correspond to several different models. All of these frames are loaded at the start of the application. This is a WIP and thus the equations that are to be drawn are hard coded using ifs, they will be later supplied by an external config file (which I've already done for the rest of the GUI). I am navigating through these frames by assigning a value to 'frame' variable and then using frame.tkraise() to raise selected variable on top of each other frame, but the math equations are not moving with the frames - the canvas with the equations stays on top of all other things in the main frame. This results in equations loaded with the last model being always on top, despite my moving the frames. How do I rephrase the code so that the equations follow the frames they are part of? I've tried asking at python@Freenode, searching through Google (~50 pages of different queries) and through documentation (which unfortunately is very scarce for Tkinter). Here's the excerpt of the code: class Model(tk.Frame):

def __init__(self, parent, controller, model_name):

tk.Frame.__init__(self, parent)

self.label = tk.Label(self, text="Model: %s" % (model_name))

self.label.grid(row=0, column=0)

#self.f = mplfig.Figure(figsize=(3.59,3.35))

print model_name

if model_name == 'LotkaVolterra':

self.eq1 = mplfig.Figure(figsize=(3.59,3.35))

#self.eq1 = self.f#.add_axes([0.0, 0.0, 1.0, 1.0], axisbg='white')

self.eq1.text(0.05, 0.875, r'$\frac{d[X]}{dt}=k_{1}[X]-k_{2}[X][Y]$', fontsize=14)

self.eq1.text(0.05, 0.750, r'$\frac{d[Y]}{dt}=-k_{3}[Y]+k_{4}[X][Y]$', fontsize=14)

self.eq1.text(0.05, 0.625, r'$A+X\rightarrow 2X \;(k_{1})$', fontsize=14)

self.eq1.text(0.05, 0.500, r'$X+Y\rightarrow 2Y \;(k_{2} \; and \; k_{4})$', fontsize=14)

self.eq1.text(0.05, 0.375, r'$Y\rightarrow B \;(k_{3})$', fontsize=14)

#self.eq1.set_xticklabels([])

#self.eq1.set_yticklabels([])

#self.eq1.set_xticks([])

#self.eq1.set_yticks([])

self.canvas = tkagg.FigureCanvasTkAgg(self.eq1, master=parent)

#self.canvas.show()

self.canvas.get_tk_widget().grid(row=1)

elif model_name == 'Brusselator':

self.eq2 = mplfig.Figure(figsize=(3.59,3.35))

#self.eq2 = self.f#.add_axes([0.0, 0.0, 1.0, 1.0], axisbg='white')

self.eq2.text(0.05, 0.875, r'$\frac{d[X]}{dt}=k_{1}[A]-k_{2}[B][X]+k_{3}[X]^{2}[Y]-k_{4}[X]$', fontsize=14)

self.eq2.text(0.05, 0.750, r'$\frac{d[Y]}{dt}=k_{2}[B][X]-k_{3}[X]^{2}[Y]$', fontsize=14)

self.eq2.text(0.05, 0.625, r'$A\rightarrow X \;(k_{1})$', fontsize=14)

self.eq2.text(0.05, 0.500, r'$B+X\rightarrow Y+D \;(k_{2})$', fontsize=14)

self.eq2.text(0.05, 0.375, r'$2X+Y\rightarrow 3X \;(k_{3})$', fontsize=14)

self.eq2.text(0.05, 0.250, r'$X\rightarrow E \;(k_{4})$', fontsize=14)

#self.eq2.set_xticklabels([])

#self.eq2.set_yticklabels([])

#self.eq2.set_xticks([])

#self.eq2.set_yticks([])

self.canvas = tkagg.FigureCanvasTkAgg(self.eq2, master=parent)

#self.canvas.show()

self.canvas.get_tk_widget().grid(row=1)

elif model_name == 'Oregonator':

self.eq3 = mplfig.Figure(figsize=(3.59,3.35))

#self.eq3 = self.f#.add_axes([0.0, 0.0, 1.0, 1.0], axisbg='white')

s

Recommend:canvas - Python Tkinter Embed Matplotlib in GUI

from matplotlib.figure import Figurefrom Tkinter import *master = Tk()master.title("Hello World!")#-------------------------------------------------------------------------------f = Figure(figsize=(5,4), dpi=100)a = f.add_subplot(11

elf.eq3.text(0.05, 0.875, r'$\frac{d[X]}{dt}=k_{1}[A][Y]-k_{2}[X][Y]+k_{3}[A][X]-2k_{4}[X]^{2}$', fontsize=13)

self.eq3.text(0.05, 0.750, r'$\frac{d[Y]}{dt}=-k_{1}[B][X]-k_{2}[X][Y]+0.5fk_{5}[B][Z]$', fontsize=13)

self.eq3.text(0.05, 0.625, r'$\frac{d[Y]}{dt}=2k_{3}[A][X]-k_{5}[B][Z]$', fontsize=13)

self.eq3.text(0.05, 0.500, r'$A+Y\rightarrow X+P \;(k_{1})$', fontsize=13)

self.eq3.text(0.05, 0.400, r'$X+Y\rightarrow 2P \;(k_{2})$', fontsize=13)

self.eq3.text(0.05, 0.300, r'$A+X\rightarrow 2X+2Z \;(k_{3})$', fontsize=13)

self.eq3.text(0.05, 0.200, r'$2X\rightarrow A+P \;(k_{4})$', fontsize=13)

self.eq3.text(0.05, 0.100, r'$B+Z\rightarrow fY \;(k_{5})$', fontsize=13)

#self.eq3.set_xticklabels([])

#self.eq3.set_yticklabels([])

#self.eq3.set_xticks([])

#self.eq3.set_yticks([])

self.canvas = tkagg.FigureCanvasTkAgg(self.eq3, master=parent)

#self.canvas.show()

self.canvas.get_tk_widget().grid(row=1)

#self.canvas = tkagg.FigureCanvasTkAgg(self.f, master=parent)

#self.canvas.show()

#self.canvas.get_tk_widget().grid(row=1)

self.grid()

As you can see, an instance of Model class will have a Label with appropriate model name (labels move correctly) and set of LaTeX equations and chemical reactions (these don't move correctly). How do I rephrase this? I've tried various approaches to no avail. Using labels would obviously solve the navigation problem, but tk.Label doesn't understand LaTeX. Neither does tk.Canvas. Only MatPlotLib Tkinter backend understands LaTeX. I want the LaTeX equations to follow tk.Labels when frames are moved upwards. python canvas matplotlib tkinter latex
  |
  this question edited Oct 13 '13 at 10:46 Steve Barnes 16.3k 2 23 40 asked Oct 13 '13 at 8:52 user2126752 39 6



 |  1 Answers

up vote 1 down vote ---Accepted---Accepted---Accepted---

When you create the canvas you do master=parent. Have you tried setting master to self?
  |
  this answer answered Oct 13 '13 at 16:25 Bryan Oakley 149k 11 156 272      Wow... Thanks! Is there a way to add relief=SUNKEN to the canvas? –  user2126752 Oct 13 '13 at 17:35      I'm getting a black frame around the last canvas (navigation works now, thanks!). Also, when I exit the application using self.quit() and self.destroy() on the mainframe window I get this: "Fatal Python error: PyEval_RestoreThread: NULL tstate". Should I present the whole code so you can take a look? –  user2126752 Oct 13 '13 at 18:01      @user2126752: I've never used matplotlib, so I probably wouldn't be able to help you with the fatal error you're seeing, as that looks to be coming from matplotlib. –  Bryan Oakley Oct 13 '13 at 19:51      Is there a better way to embed the equations? I would prefer to refrain from pictures and keep them as TeX. –  user2126752 Oct 13 '13 at 22:29      @user2126752: I have no idea. I've never needed to do that. –  Bryan Oakley Oct 14 '13 at 0:06



 | 

Recommend:python - Matplotlib figures not working after Tkinter file dialog

window for file selection. [ Edit: Turns out the distro differences here are merely because Matplotlib is using gtk3agg on Fedora and TkAgg for drawing windows on each system ] On Fedora 18: Everything works fine. On Ubuntu 12.10: Matplotl

up vote 0 down vote favorite I can post the whole GUI and files necessary to run it, if needed. I'm trying to embed math equations in frames that correspond to several different mo

------splitte line----------------------------