Reputation: 59
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')
self.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.
Upvotes: 0
Views: 143
Reputation: 385980
When you create the canvas you do master=parent
. Have you tried setting master
to self
?
Upvotes: 1