python - 调用plt.figure阻止Checkbutton更新--Tkinter

时间:2016-03-07 10:23:15

标签: python-3.x canvas matplotlib

适用于不同页面的GUI,一个是使用animate功能的图形页面,另一个页面应该启动指南,指南将取决于您在复选框中单击的内容。我创建了一个帖子,因为我认为我是没有明确定义的检查按钮,但现在我发现调用f = plt.figure()会阻止检查按钮的升级。

请参阅此代码适用于Checkbutton: - 在animate函数之前..但是由于f未在animate函数中定义,因此其他页面上的图形更新不起作用。如果我在PageThree(GRAPHIC页面)中放置animate函数和f定义,则图形更新失败。 但是当我允许f = plt.figure()时,图形很好,但是checkbutton一直保持False ......

import matplotlib
from tkinter import *
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
import tkinter as tk
from tkinter import ttk
import numpy as np
import matplotlib.animation as animation
from matplotlib import style
from matplotlib import pyplot as plt
from scipy.misc import *
import pandas as pd
import webbrowser
import subprocess
import csv
import matplotlib.ticker as mticker
import matplotlib.dates as mdates
from os import startfile
from time import time

LARGE_FONT = ("Times", 11, "bold italic")
NORM_FONT = ("Helvetica", 9)
SMALL_FONT = ("Helvetica",7)
HELP_FONT=("Times", 9 , "bold")


pullDataMS=pd.read_table("test.raw.spectrum_table.tsv", header=1)
LevelMS=pullDataMS.groupby('msLevel')

rt=pullDataMS['rt']
TIC=pullDataMS['TIC']

MS1=LevelMS.get_group('ms1')
MS2=LevelMS.get_group('ms2')

### THIS CALL overplots the update? -.- ###
#f = plt.figure(facecolor="white")**

 ######################
def animate (i):
     pullData=pd.read_table("InstrumentPerformance.txt", parse_dates=True,
                       dayfirst=True, index_col=1)
     Names_In = ['Q1','Q2','Q3']

     Inst=pullData.groupby('Instrument')

     a = f.add_subplot(2,3,1)
     a2 = f.add_subplot(2,3,2, sharex=a)
     a3=f.add_subplot(2,3,3)
     a4=f.add_subplot(2,3,4)
     a5=f.add_subplot(2,3,5)
     a7=f.add_subplot(2,6,11)
     f.subplots_adjust(wspace=0.29)
     a.clear()
     a2.clear()
     a3.clear()
     a4.clear()
     a5.clear()
     a7.clear()
     for Name in Names_In:
          Q=Inst.get_group(Name)
          y=Q['MS/MS recorded']
          x=Q['Identified peptides']

          a.plot(x, "o",label='['+Name+"]")
          a.plot(x, 'k-', lw=0.7, label='_nolegend_')

          a2.plot(y, 'o',label='['+Name+']')
          a2.plot(y, 'k-', label='_nolegend_', lw=0.7)

          a.xaxis.set_major_locator(mticker.MaxNLocator(5))
          a.xaxis.set_major_formatter(mdates.DateFormatter("%d-%m-%Y"))
          a.set_ylabel('# of unique peptide sequences')
          a2.set_ylabel('# of MS/MS spectra')
          a.legend(loc='upper left',fancybox=True, numpoints=1, shadow=True,prop={'size':9})
          a2.legend(loc='upper left',fancybox=True, numpoints=1, shadow=True,prop={'size':9})
          a.set_xlim(np.array([-20,20])+a.get_xlim())
          a.set_ylim(np.array([0,2000])+a.get_ylim())
          a2.set_xlim(np.array([-20,20])+a.get_xlim())
          a2.set_ylim(np.array([0,5000])+a.get_ylim())




class PAL3_guide(tk.Tk):

     def __init__(self,*args, **kwargs):

          tk.Tk.__init__(self, *args, **kwargs)
          container = tk.Frame(self)
          container.pack(side="top", fill="both", expand = True)
          container.grid_rowconfigure(0, weight=1)
          container.grid_columnconfigure(0, weight=1) 
          self.frames = {}

          for F in (StartPage,Tut01,PageThree):

               frame = F(container, self)

               self.frames[F] = frame

               frame.grid(row=0, column=0, sticky="nsew")

          self.show_frame(StartPage)

     def show_frame(self, cont):
          frame=self.frames[cont]
          frame.tkraise()

class StartPage(tk.Frame):
     def __init__(self,parent, controller):
          tk.Frame.__init__(self,parent, background="white")
          button_PAL = ttk.Button(self, text="Setup Guide", command=lambda:controller.show_frame(Tut01))
          button_PAL.pack()
          button2 = ttk.Button(self, text="Graph Page 1", 
                              command=lambda: controller.show_frame(PageThree))
          button2.pack()

class Tut01(tk.Frame):
     def __init__(self,parent,controller):


          tk.Frame.__init__(self,parent)       
          self.parent=parent

          self.initUI()

     def initUI(self):
          self.var=BooleanVar()
          cb=Checkbutton(self,text="show", variable=self.var, command=self.onClick)
          cb.select()
          cb.place(x=50,y=50)
     def onClick(self):
          if self.var.get()== True:
               print("hi")
          else:
               print("buh")

class PageThree(tk.Frame):

     def __init__(self,parent,controller):
          tk.Frame.__init__(self,parent)
          label = tk.Label(self, text="GraphPage", font=LARGE_FONT)
          label.pack(pady=10, padx=10)
          button1 = ttk.Button(self, text="Back to Home",
                              command=lambda: controller.show_frame(StartPage))
          button1.pack()


          canvas = FigureCanvasTkAgg(f, self)
          canvas.show()
          canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)

          toolbar=NavigationToolbar2TkAgg(canvas,self)
          toolbar.update()
          canvas._tkcanvas.pack(side=tk.TOP,fill=tk.BOTH, expand=True)


app = PAL3_guide()
app.geometry("1280x920")
ani = animation.FuncAnimation(f, animate, interval=10000)  

app.mainloop()

编辑MIXONE :是的,我尝试过这样的事情:

def animate (f):
     pullData=pd.read_table("InstrumentPerformance.txt", parse_dates=True,
                       dayfirst=True, index_col=1)
     Names_In = ['Q1','Q2','Q3']

     Inst=pullData.groupby('Instrument')

     a = f.add_subplot(2,3,1)
     a2 = f.add_subplot(2,3,2, sharex=a)...

... 并且:

class PageThree(tk.Frame):

     def __init__(self,parent,controller):
          tk.Frame.__init__(self,parent)
          label = tk.Label(self, text="GraphPage", font=LARGE_FONT)
          label.pack(pady=10, padx=10)
          button1 = ttk.Button(self, text="Back to Home",
                              command=lambda: controller.show_frame(StartPage))
          button1.pack()

          f=plt.figure()

          ani = animation.FuncAnimation(f, animate(f), interval=10000)  

          canvas = FigureCanvasTkAgg(f, self)
          canvas.show()
          canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)

          #toolbar=NavigationToolbar2TkAgg(canvas,self)
          #toolbar.update()
          #canvas._tkcanvas.pack(side=tk.TOP,fill=tk.BOTH, expand=True)


app = PAL3_guide()
app.geometry("1280x920")

app.mainloop()

在这种情况下,Checkbutton可以正常工作,但是图形没有更新,但它在开头就被绘制了。

0 个答案:

没有答案