2017-08-01 72 views
0

我正在使用Tkinter構建一個GUI,它將在一個窗口內顯示一個動畫圖形(以及其他一些小部件)。這很好,但第二個matplotlib窗口也隨着Tkinter主窗口一起打開。我怎樣才能防止這種情況發生?Tkinter打開額外的matplotlib圖

import matplotlib 
matplotlib.use('TkAgg') 
import matplotlib.pyplot as plt 
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg 
from matplotlib.figure import Figure 
import Tkinter as tk 
import ttk 

class Application(tk.Frame): 
    def __init__(self, master=None): 
     tk.Frame.__init__(self, master) 
     #self.a = Arduino() #the data is coming in from an arduino 
     self.createWidgets() 
    def createWidgets(self): 
     fig = plt.figure(figsize=(6,6)) 
     ax = fig.add_axes([0,0,1,1]) 
     canvas = FigureCanvasTkAgg(fig, master=root) 
     canvas.get_tk_widget().place(x=0, y=0) 

     self.plotbutton=tk.Button(master = root, text="plot", command=lambda: self.plot(canvas,ax)) 
     self.plotbutton.place(x=500, y=0) 

     self.quitButton = tk.Button(master=root, text="quit", command=self.quit) 
     self.quitButton.place(x=600, y=0) 

    def plot(self,canvas,ax): 
     while(1): 
      print "plotting" 
      plt.pause(0.1) 
      ax.clear()   # clear axes from previous plot 
      #values = self.getData(arduino) #in the full program, I get new theta and r data from an arduino 
      theta = [0, 1, 2, 3, 4, 5] #arbitrary axis values for testing purposes 
      r = [0, 1, 2, 3, 4, 5] 
      ax.plot(r, theta) 
      plt.xlim([0, 6]) #arbitrary axes limits 
      plt.ylim([0, 6]) 
      canvas.draw() 

    def quit(self): 
     self.master.destroy() 

root=tk.Tk() 
root.geometry("950x500+300+300") 
app=Application(master = root) 
app.mainloop() 

謝謝!

編輯:使程序進入工作示例

+0

沒有一個[MCVE]我只能猜測:我猜,因爲你在代碼中使用'plt.show()'某處,這將打開一個新窗口。如果你的情節嵌入到tkinter中,你不需要這樣做。另外,使用matplotlib的動畫功能,你的圖形會更光滑。 – Novel

+0

@Novel我沒有任何plt.show()在代碼:( 我修正了這個例子,因爲你建議雖然,也許這將是更有幫助 – janizer

回答

1

感謝您的示例。這是調用一個新窗口的plt.pause()函數(我也不知道)。改爲使用time.sleep。然而,你有一個更大的問題:在GUI中有任何while(1)循環會鎖定GUI。使用matplotlib動畫功能正確執行此操作。這是我爲另一個SO回答做出的例子。我添加了一個隨機數據發生器,所以你可以看到它無需數據連接運行:

try: 
    import Tkinter as tk 
except ImportError: 
    import tkinter as tk 
#~ import serial 
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg 
from matplotlib.figure import Figure 
from matplotlib import pyplot as plt 
import matplotlib.animation as animation 
from collections import deque 
import random 
import time 

HISTORY_LEN = 200 

class App(tk.Frame): 
    def __init__(self, master=None, **kwargs): 
     tk.Frame.__init__(self, master, **kwargs) 

     self.running = False 
     self.ani = None 

     btns = tk.Frame(self) 
     btns.pack() 

     lbl = tk.Label(btns, text="update interval (ms)") 
     lbl.pack(side=tk.LEFT) 

     self.interval = tk.Entry(btns, width=5) 
     self.interval.insert(0, '30') 
     self.interval.pack(side=tk.LEFT) 

     self.btn = tk.Button(btns, text='Start', command=self.on_click) 
     self.btn.pack(side=tk.LEFT) 

     self.fig = plt.Figure() 
     self.ax1 = self.fig.add_subplot(111) 
     self.line, = self.ax1.plot([], [], lw=2) 
     self.canvas = FigureCanvasTkAgg(self.fig,master=self) 
     self.canvas.show() 
     self.canvas.get_tk_widget().pack() 

     self.ax1.set_ylim(0,100) 
     self.ax1.set_xlim(0,500) 

    def on_click(self): 
     '''the button is a start, pause and unpause button all in one 
     this method sorts out which of those actions to take''' 
     if self.ani is None: 
      # animation is not running; start it 
      return self.start() 

     if self.running: 
      # animation is running; pause it 
      self.ani.event_source.stop() 
      self.btn.config(text='Un-Pause') 
     else: 
      # animation is paused; unpause it 
      self.ani.event_source.start() 
      self.btn.config(text='Pause') 
     self.running = not self.running 

    def start(self): 
     self.xdata = deque([], maxlen=HISTORY_LEN) 
     self.ydata = deque([], maxlen=HISTORY_LEN) 
     #~ self.arduinoData = serial.Serial('com5', 115200) 
     #~ self.arduinoData.flushInput() 
     self.ani = animation.FuncAnimation(
      self.fig, 
      self.update_graph, 
      interval=int(self.interval.get()), 
      repeat=True) 
     self.running = True 
     self.btn.config(text='Pause') 
     self.ani._start() 
     self.start_time = time.time() 
     print('started animation') 

    def update_graph(self, i): 
     self.xdata.append(i) 
     #~ self.ydata.append(int(self.arduinoData.readline())) 
     self.ydata.append(random.randrange(100)) # DEBUG 
     self.line.set_data(self.xdata, self.ydata) 
     self.ax1.set_ylim(min(self.ydata), max(self.ydata)) 
     self.ax1.set_xlim(min(self.xdata), max(self.xdata)) 
     return self.line, 

def main(): 
    root = tk.Tk() 
    app = App(root) 
    app.pack() 
    root.mainloop() 

if __name__ == '__main__': 
    main() 
+0

'後()'應該不會更有趣在這種情況下? – PRMoureu

+0

@PRMoureu你可以使用'after',但你會重新創建一個已經內置到matplotlib中的輪子,所以我沒有理由這麼做。 – Novel

+0

對不起,我沒有檢查整個代碼,你沒有使用'time .sleep'在所有... – PRMoureu