1

私はpythonが初めてなので、ご容赦ください。私のpythonスクリプトはファイルを読み取り、それを解析して配列にします。次に、matplotlib ライブラリを使用して棒グラフをプロットします。ファイル「datadump.txt」内のデータは変化し続けるため、スクリプトに無限ループを追加して、データを読み取り、解析してからプロットしました。すべて正常に動作します。問題は、マウスでウィンドウのサイズを変更しようとすると、キャンバスまたはプロットが同じままで、新しいウィンドウ サイズにサイズ変更されないことです。

キャンバスのサイズが新しいウィンドウ サイズに変更されるようにするにはどうすればよいですか?

これが私のpythonスクリプトです:

#! /usr/bin/python2.7
#! /usr/lib/pymodules/python2.7

import time
import matplotlib.pyplot as plt

# Global variables
fig = plt.figure()


def animated_graph():
    printonce = True
    while (True):
        # Make sure that the file is not currently being access by another program
        # If it is, then let the user know.
        try:
            file = open('datadump.txt', 'r')
        except IOError:
            if (printonce == True):
                print "The file is empty or does not exist."
                printonce = False

        # If the file is accessible, then execute the code below
        with file:
            file.seek(0)
            first_char = file.read(1)
            if first_char:
                # Extract all data and create a matrix containing string values
                table = [row.strip().split('\t') for row in file]
                # Close the file once the data has been extracted
                file.close()

                # If the table is not empty, then continue with the execution
                if table:
                    numrow = len(table)
                    numcol = len(table[0])

                    #print ("num of rows: " + str(numrow))
                    #print ("num of cols: " +  str(numcol))

                    tcp = 0
                    udp = 0
                    http = 0
                    dns = 0
                    icmp = 0

                    # Go thru each row and combine the total count of each protocol of for all IPs
                    for r in range(1, numrow):
                        for c in range(1, numcol):
                            if c==1:
                                tcp = tcp + int(table[r][c])
                            elif c==2:
                                udp = udp + int(table[r][c])
                            elif c==3:
                                http = http + int(table[r][c])
                            elif c==4:
                                dns = dns + int(table[r][c])
                            elif c==5:
                                icmp = icmp + int(table[r][c])


                    '''
                    print "tcp:  " + str(tcp)
                    print "udp:  " + str(udp)
                    print "http: " + str(http)
                    print "dns:  " + str(dns)
                    print "icmp: " + str(icmp)
                    '''
                    gridnumber = range(1,6)
                    labels = ["tcp", "udp", "http", "dns", "icmp"]
                    #plt.bar(gridnumber, [tcp, udp, http, dns, icmp], color="red", width=0.4, label="Total # of connections", align="center")
                    plt.clf()
                    plt.bar(1, tcp, color="red", width=0.4, label="tcp " + str(tcp), align="center")
                    plt.bar(2, udp, color="green", width=0.4, label="udp " + str(udp), align="center")
                    plt.bar(3, http, color="blue", width=0.4, label="http " + str(http), align="center")
                    plt.bar(4, dns, color="brown", width=0.4, label="dns " + str(dns), align="center")
                    plt.bar(5, icmp, color="gold", width=0.4, label="icmp " + str(icmp), align="center")
                    plt.xlim([0,8])
                    plt.xticks(gridnumber, labels)
                    plt.xlabel("Protocols")
                    plt.ylabel("Total # of packets")
                    plt.title("Number of packets in a time window of 5secs")
                    plt.legend()
                    fig.canvas.draw()
                    #time.sleep(1)

def main():
    length = 0
    print "\nOpening file 'datadump.txt' "
    printonce = True
    while (True):
        try:
            length = len(open("datadump.txt").readlines())
            break
        except IOError:
            if (printonce == True):
                print "The file is empty or does not exist."
                printonce = False

    #print "%d lines in your choosen file" % length
    win = fig.canvas.manager.window
    win.after(1, animated_graph)
    plt.figure(fig.number)
    plt.show()

main()
4

1 に答える 1

1

You need to let the GUI event loop catch its breath, and calling time.sleep() doesn't quite do it, though it's almost the thing to do.

In my code, I solve this problem with a call toplt.waitforbuttonpress(.1), where the .1 is the timeout to wait (in seconds), before allowing the call to return and continue running whatever else you have going on.

I suspect this will allow the gui to catch the resize events and process them properly.

于 2013-02-21T04:08:15.700 に答える