python - tkinter and matplotlib: windows not showing until program closes under Linux -
i've written program plots different data upon pressing different buttons. program works intended under windows, when tried port linux (red hat v6) i'm getting strange issue: window want plot not appear until after close main program. happens regardless of figure (figure 1,2 etc.) i'm trying plot to, or if try type plt.show() etc.
the program i've written 1000 lines of code, created abbreviated program has same problem. works under windows, under linux have close root window matplotlib window appear.
working code:
import matplotlib.pyplot plt tkinter import * def click(): x=['0','1','2'] plt.plot(x,x) plotgui=tk() butt1=button(plotgui,text="test", command=click).grid() plotgui.mainloop()
if reduced code still not show tk-toplevel window, add line of:
plotgui.lift() # force wm raise tk() window plotgui.mainloop()
if reduced code has problems matplotlib
-wrapper, necessary more specific on wrapping method use matplitlib
-output tkinter canvas et al.
if code attempts rely on default matplotlib
tools alike plt.show()
code suffer having 2 adjacent .mainloop()
-s -- first tk()
's -- second 1 hidden in default matplotlib
-s .show()
-- , code become hardly controllable of 2 adjacent ui-s.
co-integrated ui
for having non-adjacent ui-controllers , more benefits of co-integrated ui, try re-use backends factories plot directly on tkinter.canvas
, other plausible widgets of choice , control.
from matplotlib.backends.backend_tkagg import figurecanvastkagg
and proceed integrated gui-mvc layers on both code & matplotlib model-state / inputs-controller / visual-output.
a bit of more code co-integrated ui sample:
class supershapeframe( frame ): # user interface: def __init__( self, master = none ): frame.__init__( self, master ) self.grid() self.m = 3 self.n1 = 2 self.n1_scaling = linearscaling( ( .1, 20 ), ( 0, 200 ) ) self.n2 = 18 self.n2_scaling = linearscaling( ( .1, 20 ), ( 0, 200 ) ) self.n3 = 18 self.n3_scaling = linearscaling( ( .1, 20 ), ( 0, 200 ) ) self.fig = figure( ( 6, 6 ), dpi = 100 ) canvas = figurecanvastkagg( self.fig, master = self ) canvas.get_tk_widget().grid( row = 0, column = 0, columnspan = 4 ) label = label( self, text = 'm' ) label.grid( row = 1, column = 1 ) self.m_slider = scale( self, from_ = 1, = 20, \ orient = horizontal, command = lambda : self.update_m() \ ) self.m_slider.grid( row = 1, column = 2 ) label = label( self, text = 'n1' ) label.grid( row = 2, column = 1 ) self.n1_slider = scale( self, from_ = 0, = 200, \ orient = horizontal, command = lambda : self.update_n1() \ ) self.n1_slider.grid( row = 2, column = 2 ) label = label( self, text = 'n2' ) label.grid( row = 3, column = 1 ) self.n2_slider = scale( self, from_ = 0, = 200, \ orient = horizontal, command = lambda : self.update_n2() \ ) self.n2_slider.grid( row = 3, column = 2 ) label = label( self, text = 'n3' ) label.grid( row = 4, column = 1 ) self.n3_slider = scale( self, from_ = 0, = 200, orient = horizontal, command = lambda : self.update_n3() \ ) self.n3_slider.grid( row = 4, column = 2 ) self.draw_figure() # >>> ================================================================ draw first appearance of instance def update_m( self ): self.m = self.m_slider.get() self.refresh_figure() # >>> .-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. update def update_n1( self ): self.n1 = self.n1_scaling.dst_to_src( self.n1_slider.get() ) self.refresh_figure() # >>> .-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. update def update_n2( self ): self.n2 = self.n2_scaling.dst_to_src( self.n2_slider.get() ) self.refresh_figure() # >>> .-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. update def update_n3(self): self.n3 = self.n3_scaling.dst_to_src( self.n3_slider.get() ) self.refresh_figure() # >>> .-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. update def refresh_figure( self ): # <<< .-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. update actual appearance of instance r = supershape_radius( self.phi, 1, 1, self.m, self.n1, self.n2, self.n3 ) # .calc new polar values in radius dimension self.lines.set_ydata( r ) # .mod <lines>, selectively <lines>.set_ydata() coordinates self.fig.canvas.draw_idle() # .gui mvc-visual part update via <self>.<fig>.<canvas>.draw_idle() def draw_figure( self ): # <<< =============================================================== draw first appearance of instance self.phi = np.linspace( 0, 2 * np.pi, 1024 ) # .sto <phi> np.array static fi-coordinates r = supershape_radius( self.phi, 1, 1, self.m, self.n1, self.n2, self.n3 ) ax = self.fig.add_subplot( 111, polar = true ) # self.lines, = ax.plot( self.phi, r, lw = 3. ) # .sto <lines> alistoflines .plot() function self.fig.canvas.draw() # .gui mvc-visual part, enforce first visual output via <self>.<fig>.<canvas>.draw() def tkdemo(): # finally, set , start our user interface: """ help: cookbook: tk-gui-mvc via supershape example tests: tkdemo() """ root = tk() root.lift() root.protocol( 'wm_delete_window', root.quit() ) # [x]-overide --------------------------- app = supershapeframe( root ) # <<<--- pass <root> app.master.title( 'cookbook: tk-gui-mvc via supershape' ) app.mainloop() pass
full-code [halldinz0r] copy/paste re-testing as-is:
####################################################################### # ### ### tkdemo() ### ### kca_plot_intk ##################################################### # tk() gui ###################################################################################################### _plot_intk tkdemo() ################# matplotlib.backends.backend_tkagg import figurecanvastkagg matplotlib.figure import figure def supershape_radius( phi, a, b, m, n1, n2, n3 ): # define function supershape curve using following code: theta = .25 * m * phi cos = np.fabs( np.cos( theta ) / ) ** n2 sin = np.fabs( np.sin( theta ) / b ) ** n3 r = ( cos + sin ) ** ( -1. / n1 ) r /= np.max( r ) return r class linearscaling( object ): # define utility object linearly scale range follows: def __init__( self, src_range, dst_range ): self.src_start, src_diff = src_range[0], src_range[1] - src_range[0] self.dst_start, dst_diff = dst_range[0], dst_range[1] - dst_range[0] self.src_to_dst_coeff = dst_diff / src_diff self.dst_to_src_coeff = src_diff / dst_diff def src_to_dst( self, x ): return ( x - self.src_start ) * self.src_to_dst_coeff + self.dst_start def dst_to_src( self, x ): return ( x - self.dst_start) * self.dst_to_src_coeff + self.src_start class supershapeframe( frame ): # user interface: def __init__( self, master = none ): frame.__init__( self, master ) self.grid() self.m = 3 self.n1 = 2 self.n1_scaling = linearscaling( ( .1, 20 ), ( 0, 200 ) ) self.n2 = 18 self.n2_scaling = linearscaling( ( .1, 20 ), ( 0, 200 ) ) self.n3 = 18 self.n3_scaling = linearscaling( ( .1, 20 ), ( 0, 200 ) ) self.fig = figure( ( 6, 6 ), dpi = 100 ) canvas = figurecanvastkagg( self.fig, master = self ) canvas.get_tk_widget().grid( row = 0, column = 0, columnspan = 4 ) label = label( self, text = 'm' ) label.grid( row = 1, column = 1 ) self.m_slider = scale( self, from_ = 1, = 20, \ orient = horizontal, command = lambda : self.update_m() \ ) self.m_slider.grid( row = 1, column = 2 ) label = label( self, text = 'n1' ) label.grid( row = 2, column = 1 ) self.n1_slider = scale( self, from_ = 0, = 200, \ orient = horizontal, command = lambda : self.update_n1() \ ) self.n1_slider.grid( row = 2, column = 2 ) label = label( self, text = 'n2' ) label.grid( row = 3, column = 1 ) self.n2_slider = scale( self, from_ = 0, = 200, \ orient = horizontal, command = lambda : self.update_n2() \ ) self.n2_slider.grid( row = 3, column = 2 ) label = label( self, text = 'n3' ) label.grid( row = 4, column = 1 ) self.n3_slider = scale( self, from_ = 0, = 200, orient = horizontal, command = lambda : self.update_n3() \ ) self.n3_slider.grid( row = 4, column = 2 ) self.draw_figure() # >>> ================================================================ draw first appearance of instance def update_m( self ): self.m = self.m_slider.get() self.refresh_figure() # >>> .-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. update def update_n1( self ): self.n1 = self.n1_scaling.dst_to_src( self.n1_slider.get() ) self.refresh_figure() # >>> .-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. update def update_n2( self ): self.n2 = self.n2_scaling.dst_to_src( self.n2_slider.get() ) self.refresh_figure() # >>> .-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. update def update_n3(self): self.n3 = self.n3_scaling.dst_to_src( self.n3_slider.get() ) self.refresh_figure() # >>> .-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. update def refresh_figure( self ): # <<< .-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. update actual appearance of instance r = supershape_radius( self.phi, 1, 1, self.m, self.n1, self.n2, self.n3 ) # .calc new polar values in radius dimension self.lines.set_ydata( r ) # .mod <lines>, selectively <lines>.set_ydata() coordinates self.fig.canvas.draw_idle() # .gui mvc-visual part update via <self>.<fig>.<canvas>.draw_idle() def draw_figure( self ): # <<< =============================================================== draw first appearance of instance self.phi = np.linspace( 0, 2 * np.pi, 1024 ) # .sto <phi> np.array static fi-coordinates r = supershape_radius( self.phi, 1, 1, self.m, self.n1, self.n2, self.n3 ) ax = self.fig.add_subplot( 111, polar = true ) # self.lines, = ax.plot( self.phi, r, lw = 3. ) # .sto <lines> alistoflines .plot() function self.fig.canvas.draw() # .gui mvc-visual part, enforce first visual output via <self>.<fig>.<canvas>.draw() def tkdemo(): # finally, set , start our user interface: """ help: cookbook: tk-gui-mvc via supershape example tests: tkdemo() """ root = tk() root.lift() root.protocol( 'wm_delete_window', root.quit() ) # [x]-overide --------------------------- app = supershapeframe( root ) # <<<--- pass <root> app.master.title( 'cookbook: tk-gui-mvc via supershape' ) app.mainloop() pass ### ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ _plot_intk tkdemo() ^^^^^^^^^^^^^^^^^^
Comments
Post a Comment