使用python3在GUI中线程化多个动画图

问题描述 投票:1回答:1

下面的代码应按原样运行。我想在其中绘制动画的三个类(GUI中的页面)。在每个类(或页面)中,我将访问一个远程文件,其中实时数据是由不同的音频编解码器和网络干扰生成的。因此,每个类(页面)都需要访问此实时数据并进行绘制。

问题是只有我的最后一个情节才用实时数据进行动画处理。前两个图是静态的,它们不会获取或更新当前数据。我试图在每个类的单独线程中运行绘图。看来我最后创建的matplotlib画布是唯一可以正确设置动画的画布,canvas3 = FigureCanvasTkAgg(f, self)

在此简化的代码版本中,我随机生成实时数据,但是它使用访问本地文本文件sampleData.txt的一些逻辑,该文件也包含在pyhton3脚本下面。

我在定义Class MyApp(tk.Tk):时主要在python文件的底部出现问题。绘图有效的类别为Class PageThree(),而当前无效的类别为Class PageOne()Class PageTwo()。如果单击“ PageThree”按钮,您将看到有效的动画图。请帮助我获得其他地块的动画。我不确定问题是线程,matplotlib画布还是ani1 = animation.FuncAnimation(f, animate, interval=1000)函数引起的。我从命令行($ python3 gui2.py)构建,也没有收到任何错误或警告。

谢谢!

gui2.py

#!/usr/bin/env python3

import paramiko, threading
import time, os, subprocess
from subprocess import Popen
import sys
#if not sys.warnoptions:
#   import warnings
#   warnings.simplefilter("ignore")

import matplotlib
matplotlib.use("TkAgg")

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
import matplotlib.animation as animation
from matplotlib import style

import tkinter as tk
from tkinter import ttk

# Just being used to debug plots
import random


LARGE_FONT=("Verdana", 12)
style.use("ggplot")

f = Figure(figsize=(9,6), dpi=100)
aPlot = f.add_subplot(211)
aPlot2 = f.add_subplot(212)

f2 = Figure(figsize=(9,6), dpi=100)
bPlot = f2.add_subplot(211)
bPlot2 = f2.add_subplot(212)

###==========================================================================================
### BEGIN FUNCS 4 FUN ###++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


# Function to execute the C++ code over ssh (Adaptive mode is default):
def start_ssh():
    print("SSH started")


# Function to update live labels
def label_reader(label):
    def reader():
        label.config(text="Mode: "+ str(mode[-1]))
        label.after(100, reader)
    reader()

# Function to kill the processes running the C++ networking code:
def clear():
    print("processes closed")


# Function to execute the C++ code over ssh using only Mode 1:
def start_ssh_singleMode():
        print("SSH single mode started")


# Function to setup interference and/or set it back to 0%:
def interference_setup():
        print("Interference setup")


# Function to add a hard-coded amount of interference:
def add_interference():
        print("Interference added")


global mode
mode = ["0"]
global plossRate
global counter, counter2
# Plot to animate
def animate(i):

    pullData = open("sampleData.txt", "r").read()
    dataList = pullData.split('\n')
    remote_file = dataList
    curFrame = []
    recFrame = []
    #global mode
    #mode = []
    #bytesRead = []
    #missingFrames = []
    plossRate = []
    counter = []
    counter2 = []
    counter2.append(0)
    value = 0
    for eachLine in remote_file:
        if len(eachLine)>1:
            value=value+1
            #a, b, c, d, e = eachLine.split(',')
            #curFrame.append(int(a))
            #recFrame.append(int(b))
            mode.append(random.randint(1,3))
            #bytesRead.append(int(d))
            #missingFrames.append(int(e))
            plossRate.append(random.randint(0,90))
            counter.append(int(value))
            counter2.append(int(value))
            #print("mode = " + str(c) + " lastFrame =  " + str(b) + "  conter = " + str(value))

    aPlot.clear()
    aPlot2.clear()
    aPlot.plot(counter, plossRate)
    aPlot.set_title('Packet Loss Rate')
    aPlot.set_ylabel('Percentage')
    aPlot2.plot(counter, mode[-counter[-1]:], 'bo')
    #aPlot2.axis(0,counter[-1],0,3)
    aPlot2.set_title('Current Audio Mode')
    aPlot2.set_ylabel('mode')


    #finally:
    #remote_file.close()


# def make_plot(i):
#   pullData = open("sampleData.txt", "r").read()
#   dataList = pullData.split('\n')
#   remote_file = dataList
#   curFrame = []
#   recFrame = []
#   #global mode
#   #mode = []
#   #bytesRead = []
#   #missingFrames = []
#   plossRate = []
#   counter = []
#   counter2 = []
#   counter2.append(0)
#   value = 0
#   for eachLine in remote_file:
#       if len(eachLine)>1:
#           value=value+1
#           #a, b, c, d, e = eachLine.split(',')
#           #curFrame.append(int(a))
#           #recFrame.append(int(b))
#           mode.append(random.randint(1,3))
#           #bytesRead.append(int(d))
#           #missingFrames.append(int(e))
#           plossRate.append(random.randint(0,90))
#           counter.append(int(value))
#           counter2.append(int(value))
#           #print("mode = " + str(c) + " lastFrame =  " + str(b) + "  conter = " + str(value))
#
#   bPlot.clear()
#   bPlot2.clear()
#   bPlot.plot(counter, plossRate)
#   bPlot.set_title('Packet Loss Rate')
#   bPlot.set_ylabel('Percentage')
#   bPlot2.plot(counter, mode[-counter[-1]:], 'bo')
#   #aPlot2.axis(0,counter[-1],0,3)
#   bPlot2.set_title('Current Audio Mode')
#   bPlot2.set_ylabel('mode')


# Start plot
# def start_plot(self):
#   canvas = FigureCanvasTkAgg(f, self)
#   canvas.draw()
#   canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)

### END FUNCS 4 FUN ###++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
###==========================================================================================


###==========================================================================================
### BEGIN MAIN CLASS FOR NETWORKUP APP ###+++++++++++++++++++++++++++++++++++++++++++++++++++
class MyApp(tk.Tk):


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

        tk.Tk.__init__(self, *args, **kwargs)
        #tk.Tk.iconbitmap(self, default="logo-no-halo-sm.png")
        tk.Tk.wm_title(self, "Network Up")

        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, PageOne, PageTwo, 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)
        label = ttk.Label(self, text="Home Page", font=LARGE_FONT)
        label.pack(pady=10, padx=10)

        button1 = ttk.Button(self, text="PageOne", command=lambda: controller.show_frame(PageOne))
        button1.pack()

        button2 = ttk.Button(self, text="PageTwo", command=lambda: controller.show_frame(PageTwo))
        button2.pack()

        button3 = ttk.Button(self, text="PageThree", command=lambda: controller.show_frame(PageThree))
        button3.pack()

class PageOne(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = ttk.Label(self, text="Audio (No Interference)", font=LARGE_FONT)
        label.pack(pady=10, padx=10)

        button1 = ttk.Button(self, text="Back to Home", command=lambda:[controller.show_frame(StartPage), clear()])
        button1.pack()

        button_start = ttk.Button(self, text="Play", command=lambda: start_ssh)
        button_start.pack()

        mode_label = ttk.Label(self, text="Mode 1")
        mode_label.pack()
        label_reader(mode_label)

        #time.sleep(.5)
        # plot_thread1 = threading.Thread(target=make_plot(self))
        # plot_thread1.daemon = True
        # plot_thread1.start()

        # canvas1 = FigureCanvasTkAgg(f, self)
        # canvas1.draw()
        # canvas1.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)
        # Just trying something different here
        self.start_plot()

    def start_plot(self):
        plot_thread1 = threading.Thread(target=animate(self))
        plot_thread1.daemon = False
        plot_thread1.start()
        canvas = FigureCanvasTkAgg(f, self)
        canvas.draw()
        canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)



class PageTwo(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = ttk.Label(self, text="Audio (Interference)", font=LARGE_FONT)
        label.pack(pady=10, padx=10)

        button1 = ttk.Button(self, text="Back to Home", command=lambda: [controller.show_frame(StartPage), clear(), interference_setup()])
        button1.pack()

        button_start_mode1 = ttk.Button(self, text="Play", command=lambda: [start_ssh_singleMode(), add_interference()])
        button_start_mode1.pack()

        mode_label = ttk.Label(self, text="Mode 1")
        mode_label.pack()
        label_reader(mode_label)

        plot_thread2 = threading.Thread(target=animate(self))
        plot_thread2.daemon = True
        plot_thread2.start()

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



class PageThree(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = ttk.Label(self, text="Audio (Interference and Adaptive Codec)", font=LARGE_FONT)
        label.pack(pady=10, padx=10)

        button1 = ttk.Button(self, text="Back to Home", command=lambda: [interference_setup(), clear(), controller.show_frame(StartPage)])
        button1.pack()

        button_start2 = ttk.Button(self, text="Play", command=lambda: [start_ssh(), add_interference()])
        button_start2.pack()


        mode_label = ttk.Label(self, text="Mode 1")
        mode_label.pack()
        label_reader(mode_label)


        plot_thread3 = threading.Thread(target=animate(self))
        plot_thread3.daemon = True
        plot_thread3.start()

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

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


app = MyApp()
ani1 = animation.FuncAnimation(f, animate, interval=1000)
#ani2 = animation.FuncAnimation(f2, make_plot, interval=1000)

app.mainloop()

这是我在某些逻辑中使用的文本文件。它需要放置在运行python程序所在的目录中。

sampleData.txt

1,1
2,3
3,4
4,3
5,5
6,7
7,7
8,14
9,30
10,23
11,5
12,4
13,27
14,9
15,14
16,16
17,2
18,1
19,5
20,2
python-3.x multithreading matplotlib animation tkinter
1个回答
0
投票

我终于弄清楚了如何使所有绘图有效并进行实时更新。事实证明,在GUI的不同页面上,每个绘图都需要一个图。并且我删除了数字的线程,因为它导致了实际应用程序中的性能问题。事实证明,它无需线程即可更快,响应速度更快。完整的代码版本位于底部:

f = Figure(figsize=(9,6), dpi=100)
aPlot = f.add_subplot(211)
aPlot2 = f.add_subplot(212)


f2 = Figure(figsize=(9,6), dpi=100)
bPlot = f2.add_subplot(211)
bPlot2 = f2.add_subplot(212)

f0 = Figure(figsize=(9,6), dpi=100)
cPlot = f0.add_subplot(211)
cPlot2 = f0.add_subplot(212)

f4 = Figure(figsize=(9,6), dpi=100)
dPlot = f4.add_subplot(211)
dPlot2 = f4.add_subplot(212)

然后,我需要一个不同的功能来对每个人物进行动画处理。因此,这是很多冗余代码。必须有更好的方法来完成此任务。

def animate(i):

    pullData = open("sampleData.txt", "r").read()
    dataList = pullData.split('\n')
    remote_file = dataList
    curFrame = []
    recFrame = []
    #global mode
    #mode = []
    #bytesRead = []
    #missingFrames = []
    plossRate = []
    counter = []
    counter2 = []
    counter2.append(0)
    #print("i is: " + str(i))
    value = 0
    for eachLine in remote_file:
        if len(eachLine)>1:
            value=value+1
            #a, b, c, d, e = eachLine.split(',')
            #curFrame.append(int(a))
            #recFrame.append(int(b))
            mode.append(random.randint(1,3))
            #bytesRead.append(int(d))
            #missingFrames.append(int(e))
            plossRate.append(random.randint(0,90))
            counter.append(int(value))
            counter2.append(int(value))
            #print("mode = " + str(c) + " lastFrame =  " + str(b) + "  conter = " + str(value))

    aPlot.clear()
    aPlot2.clear()
    aPlot.plot(counter, plossRate)
    aPlot.set_title('Packet Loss Rate')
    aPlot.set_ylabel('Percentage')
    aPlot2.plot(counter, mode[-counter[-1]:], 'bo')
    #aPlot2.axis(0,counter[-1],0,3)
    aPlot2.set_title('Current Audio Mode')
    aPlot2.set_ylabel('mode')



    #finally:
    #pullData.close()


def make_plot(i):
    pullData = open("sampleData.txt", "r").read()
    dataList = pullData.split('\n')
    remote_file = dataList
    curFrame = []
    recFrame = []
    #global mode
    #mode = []
    #bytesRead = []
    #missingFrames = []
    #plossRate = []
    counter = []
    counter2 = []
    counter2.append(0)
    value = 0
    for eachLine in remote_file:
        if len(eachLine)>1:
            value=value+1
            #a, b, c, d, e = eachLine.split(',')
            #curFrame.append(int(a))
            #recFrame.append(int(b))
            mode.append(random.randint(1,3))
            #bytesRead.append(int(d))
            #missingFrames.append(int(e))
            plossRate.append(random.randint(0,90))
            counter.append(int(value))
            counter2.append(int(value))
            #print("mode = " + str(c) + " lastFrame =  " + str(b) + "  conter = " + str(value))

    bPlot.clear()
    bPlot2.clear()
    bPlot.plot(counter, plossRate[-counter[-1]:])
    bPlot.set_title('Packet Loss Rate')
    bPlot.set_ylabel('Percentage')
    bPlot2.plot(counter, mode[-counter[-1]:], 'bo')
    #aPlot2.axis(0,counter[-1],0,3)
    bPlot2.set_title('Current Audio Mode')
    bPlot2.set_ylabel('mode')


def base_plot(i):
    pullData = open("sampleData.txt", "r").read()
    dataList = pullData.split('\n')
    remote_file = dataList
    curFrame = []
    recFrame = []
    #global mode
    #mode = []
    #bytesRead = []
    #missingFrames = []
    #plossRate = []
    counter = []
    counter2 = []
    counter2.append(0)
    value = 0
    for eachLine in remote_file:
        if len(eachLine)>1:
            value=value+1
            #a, b, c, d, e = eachLine.split(',')
            #curFrame.append(int(a))
            #recFrame.append(int(b))
            mode.append(random.randint(1,3))
            #bytesRead.append(int(d))
            #missingFrames.append(int(e))
            plossRate.append(random.randint(0,90))
            counter.append(int(value))
            counter2.append(int(value))
            #print("mode = " + str(c) + " lastFrame =  " + str(b) + "  conter = " + str(value))

    cPlot.clear()
    cPlot2.clear()
    cPlot.plot(counter, plossRate[-counter[-1]:])
    cPlot.set_title('Packet Loss Rate')
    cPlot.set_ylabel('Percentage')
    cPlot2.plot(counter, mode[-counter[-1]:], 'bo')
    #aPlot2.axis(0,counter[-1],0,3)
    cPlot2.set_title('Current Audio Mode')
    cPlot2.set_ylabel('mode')

def livePlot(i):
    pullData = open("sampleData.txt", "r").read()
    dataList = pullData.split('\n')
    remote_file = dataList
    curFrame = []
    recFrame = []
    #global mode
    #mode = []
    #bytesRead = []
    #missingFrames = []
    #plossRate = []
    counter = []
    counter2 = []
    counter2.append(0)
    value = 0
    for eachLine in remote_file:
        if len(eachLine)>1:
            value=value+1
            #a, b, c, d, e = eachLine.split(',')
            #curFrame.append(int(a))
            #recFrame.append(int(b))
            mode.append(random.randint(1,3))
            plossRate.append(random.randint(0,90))
            counter.append(int(value))
            counter2.append(int(value))
            #print("mode = " + str(c) + " lastFrame =  " + str(b) + "  conter = " + str(value))

    dPlot.clear()
    dPlot2.clear()
    dPlot.plot(counter, plossRate[-counter[-1]:])
    dPlot.set_title('Packet Loss Rate')
    dPlot.set_ylabel('Percentage')
    dPlot2.plot(counter, mode[-counter[-1]:], 'bo')
    dPlot2.set_title('Current Audio Mode')
    dPlot2.set_ylabel('mode')

然后在应用程序主循环中,我必须调用所有这些:

ani1 = animation.FuncAnimation(f, animate, interval=1000)
ani2 = animation.FuncAnimation(f2, make_plot, interval=1000)
ani3 = animation.FuncAnimation(f0, base_plot, interval=1000)
ani4 = animation.FuncAnimation(f4, livePlot, interval=1000)

这是python代码的有效版本:

gui3.py

#!/usr/bin/env python3

import paramiko, threading
import time, os, subprocess
from subprocess import Popen
import sys
# if not sys.warnoptions:
#   import warnings
#   warnings.simplefilter("ignore")

import matplotlib
matplotlib.use("TkAgg")

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
import matplotlib.animation as animation
from matplotlib import style

import tkinter as tk
from tkinter import ttk

# Just being used to debug plots
import random
from matplotlib import pyplot as plt


LARGE_FONT=("Verdana", 12)
style.use("ggplot")

f = Figure()
aPlot = f.add_subplot(211)
aPlot2 = f.add_subplot(212)


f2 = Figure(figsize=(9,6), dpi=100)
bPlot = f2.add_subplot(211)
bPlot2 = f2.add_subplot(212)

f0 = Figure(figsize=(9,6), dpi=100)
cPlot = f0.add_subplot(211)
cPlot2 = f0.add_subplot(212)

f4 = Figure(figsize=(9,6), dpi=100)
dPlot = f4.add_subplot(211)
dPlot2 = f4.add_subplot(212)


###==========================================================================================
### BEGIN FUNCS 4 FUN ###++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


# Function to execute the C++ code over ssh (Adaptive mode is default):
def start_ssh():
    print("SSH started")


# Function to update live labels
def label_reader(label):
    def reader():
        label.config(text="Mode: "+ str(mode[-1]))
        label.after(100, reader)
    reader()

def loss_label_reader(label):
    def reader():
        label.config(text="Loss: "+ str(plossRate[-1]))
        label.after(100, reader)
    reader()

# Function to kill the processes running the C++ networking code:
def clear():
    print("processes closed")


# Function to execute the C++ code over ssh using only Mode 1:
def start_ssh_singleMode():
        print("SSH single mode started")


# Function to setup interference and/or set it back to 0%:
def interference_setup():
        print("Interference setup")


# Function to add a hard-coded amount of interference:
def add_interference():
        print("Interference added")


global mode
mode = ["0"]
global plossRate
plossRate = ["0"]
global counter, counter2
# Plot to animate
def animate(i):

    pullData = open("sampleData.txt", "r").read()
    dataList = pullData.split('\n')
    remote_file = dataList
    curFrame = []
    recFrame = []
    #global mode
    #mode = []
    #bytesRead = []
    #missingFrames = []
    plossRate = []
    counter = []
    counter2 = []
    counter2.append(0)
    #print("i is: " + str(i))
    value = 0
    for eachLine in remote_file:
        if len(eachLine)>1:
            value=value+1
            #a, b, c, d, e = eachLine.split(',')
            #curFrame.append(int(a))
            #recFrame.append(int(b))
            mode.append(random.randint(1,3))
            #bytesRead.append(int(d))
            #missingFrames.append(int(e))
            plossRate.append(random.randint(0,90))
            counter.append(int(value))
            counter2.append(int(value))
            #print("mode = " + str(c) + " lastFrame =  " + str(b) + "  conter = " + str(value))

    aPlot.clear()
    aPlot2.clear()
    aPlot.plot(counter, plossRate)
    aPlot.set_title('Packet Loss Rate')
    aPlot.set_ylabel('Percentage')
    aPlot2.plot(counter, mode[-counter[-1]:], 'bo')
    #aPlot2.axis(0,counter[-1],0,3)
    aPlot2.set_title('Current Audio Mode')
    aPlot2.set_ylabel('mode')



def make_plot(i):
    pullData = open("sampleData.txt", "r").read()
    dataList = pullData.split('\n')
    remote_file = dataList
    curFrame = []
    recFrame = []
    #global mode
    #mode = []
    #bytesRead = []
    #missingFrames = []
    #plossRate = []
    counter = []
    counter2 = []
    counter2.append(0)
    value = 0
    for eachLine in remote_file:
        if len(eachLine)>1:
            value=value+1
            #a, b, c, d, e = eachLine.split(',')
            #curFrame.append(int(a))
            #recFrame.append(int(b))
            mode.append(random.randint(1,3))
            #bytesRead.append(int(d))
            #missingFrames.append(int(e))
            plossRate.append(random.randint(0,90))
            counter.append(int(value))
            counter2.append(int(value))
            #print("mode = " + str(c) + " lastFrame =  " + str(b) + "  conter = " + str(value))

    bPlot.clear()
    bPlot2.clear()
    bPlot.plot(counter, plossRate[-counter[-1]:])
    bPlot.set_title('Packet Loss Rate')
    bPlot.set_ylabel('Percentage')
    bPlot2.plot(counter, mode[-counter[-1]:], 'bo')
    #aPlot2.axis(0,counter[-1],0,3)
    bPlot2.set_title('Current Audio Mode')
    bPlot2.set_ylabel('mode')


def base_plot(i):
    pullData = open("sampleData.txt", "r").read()
    dataList = pullData.split('\n')
    remote_file = dataList
    curFrame = []
    recFrame = []
    #global mode
    #mode = []
    #bytesRead = []
    #missingFrames = []
    #plossRate = []
    counter = []
    counter2 = []
    counter2.append(0)
    value = 0
    for eachLine in remote_file:
        if len(eachLine)>1:
            value=value+1
            #a, b, c, d, e = eachLine.split(',')
            #curFrame.append(int(a))
            #recFrame.append(int(b))
            mode.append(random.randint(1,3))
            #bytesRead.append(int(d))
            #missingFrames.append(int(e))
            plossRate.append(random.randint(0,90))
            counter.append(int(value))
            counter2.append(int(value))
            #print("mode = " + str(c) + " lastFrame =  " + str(b) + "  conter = " + str(value))

    cPlot.clear()
    cPlot2.clear()
    cPlot.plot(counter, plossRate[-counter[-1]:])
    cPlot.set_title('Packet Loss Rate')
    cPlot.set_ylabel('Percentage')
    cPlot2.plot(counter, mode[-counter[-1]:], 'bo')
    #aPlot2.axis(0,counter[-1],0,3)
    cPlot2.set_title('Current Audio Mode')
    cPlot2.set_ylabel('mode')

def livePlot(i):
    pullData = open("sampleData.txt", "r").read()
    dataList = pullData.split('\n')
    remote_file = dataList
    curFrame = []
    recFrame = []
    #global mode
    #mode = []
    #bytesRead = []
    #missingFrames = []
    #plossRate = []
    counter = []
    counter2 = []
    counter2.append(0)
    value = 0
    for eachLine in remote_file:
        if len(eachLine)>1:
            value=value+1
            #a, b, c, d, e = eachLine.split(',')
            #curFrame.append(int(a))
            #recFrame.append(int(b))
            mode.append(random.randint(1,3))
            #bytesRead.append(int(d))
            #missingFrames.append(int(e))
            plossRate.append(random.randint(0,90))
            counter.append(int(value))
            counter2.append(int(value))
            #print("mode = " + str(c) + " lastFrame =  " + str(b) + "  conter = " + str(value))

    dPlot.clear()
    dPlot2.clear()
    dPlot.plot(counter, plossRate[-counter[-1]:])
    dPlot.set_title('Packet Loss Rate')
    dPlot.set_ylabel('Percentage')
    dPlot2.plot(counter, mode[-counter[-1]:], 'bo')
    #aPlot2.axis(0,counter[-1],0,3)
    dPlot2.set_title('Current Audio Mode')
    dPlot2.set_ylabel('mode')


### END FUNCS 4 FUN ###++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
###==========================================================================================


###==========================================================================================
### BEGIN MAIN CLASS FOR NETWORKUP APP ###+++++++++++++++++++++++++++++++++++++++++++++++++++
class MyApp(tk.Tk):


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

        tk.Tk.__init__(self, *args, **kwargs)
        #tk.Tk.iconbitmap(self, default="logo-no-halo-sm.png")
        tk.Tk.wm_title(self, "Network Up")

        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, PageOne, PageTwo, PageThree, PageFour):

            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)
        label = ttk.Label(self, text="Home Page", font=LARGE_FONT)
        label.pack(pady=10, padx=10)

        button1 = ttk.Button(self, text="PageOne", command=lambda: controller.show_frame(PageOne))
        button1.pack()

        button2 = ttk.Button(self, text="PageTwo", command=lambda: controller.show_frame(PageTwo))
        button2.pack()

        button3 = ttk.Button(self, text="PageThree", command=lambda: controller.show_frame(PageThree))
        button3.pack()

        button4 = ttk.Button(self, text="PageFour", command=lambda: controller.show_frame(PageFour))
        button4.pack(pady=5, padx=5)

        canvas0 = FigureCanvasTkAgg(f, self)
        self.canvas = canvas0

class PageOne(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = ttk.Label(self, text="Audio (No Interference)", font=LARGE_FONT)
        label.pack(pady=10, padx=10)

        button1 = ttk.Button(self, text="Back to Home", command=lambda:[controller.show_frame(StartPage), clear()])
        button1.pack()

        button_start = ttk.Button(self, text="Play", command=lambda: start_ssh)
        button_start.pack()

        loss_label = ttk.Label(self, text="Loss 0")
        loss_label.pack()
        loss_label_reader(loss_label)

        mode_label = ttk.Label(self, text="Mode 1")
        mode_label.pack()
        label_reader(mode_label)

        canvas1 = FigureCanvasTkAgg(f0, self)
        canvas1.draw()
        canvas1.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)
        self.canvas = canvas1



class PageTwo(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = ttk.Label(self, text="Audio (Interference)", font=LARGE_FONT)
        label.pack(pady=10, padx=10)

        button1 = ttk.Button(self, text="Back to Home", command=lambda: [controller.show_frame(StartPage), clear(), interference_setup()])
        button1.pack()

        button_start_mode1 = ttk.Button(self, text="Play", command=lambda: [start_ssh_singleMode(), add_interference()])
        button_start_mode1.pack()

        loss_label = ttk.Label(self, text="Loss 0")
        loss_label.pack()
        loss_label_reader(loss_label)

        mode_label = ttk.Label(self, text="Mode 1")
        mode_label.pack()
        label_reader(mode_label)

        canvas2 = FigureCanvasTkAgg(f2, self)
        canvas2.draw()
        canvas2.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)
        self.canvas = canvas2



class PageThree(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = ttk.Label(self, text="Audio (Interference and Adaptive Codec)", font=LARGE_FONT)
        label.pack(pady=10, padx=10)

        button1 = ttk.Button(self, text="Back to Home", command=lambda: [interference_setup(), clear(), controller.show_frame(StartPage)])
        button1.pack()

        button_start2 = ttk.Button(self, text="Play", command=lambda: [start_ssh(), add_interference()])
        button_start2.pack()

        loss_label = ttk.Label(self, text="Loss 0")
        loss_label.pack()
        loss_label_reader(loss_label)

        mode_label = ttk.Label(self, text="Mode 1")
        mode_label.pack()
        label_reader(mode_label)

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


class PageFour(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = ttk.Label(self, text="Audio (Interference and Adaptive Codec)", font=LARGE_FONT)
        label.pack(pady=10, padx=10)

        button1 = ttk.Button(self, text="Back to Home", command=lambda: [interference_setup(), clear(), controller.show_frame(StartPage)])
        button1.pack()

        button_start2 = ttk.Button(self, text="Play", command=lambda: [start_ssh(), add_interference()])
        button_start2.pack()

        w2 = tk.Scale(self, from_=0, to=90, orient='horizontal')
        w2.pack()

        loss_label = ttk.Label(self, text="Loss 0")
        loss_label.pack()
        loss_label_reader(loss_label)

        mode_label = ttk.Label(self, text="Mode 1")
        mode_label.pack()
        label_reader(mode_label)


        canvas4 = FigureCanvasTkAgg(f4, self)
        canvas4.draw()
        canvas4.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)
        self.canvas = canvas4



app = MyApp()
app.geometry("1280x720")
ani1 = animation.FuncAnimation(f, animate, interval=1000)
ani2 = animation.FuncAnimation(f2, make_plot, interval=1000)
ani3 = animation.FuncAnimation(f0, base_plot, interval=1000)
ani4 = animation.FuncAnimation(f4, livePlot, interval=1000)

app.mainloop()

并且根据逻辑,您仍然需要添加sampleData.txt,可以在上面的帖子问题中找到。

热门问题
推荐问题
最新问题