首页 文章

为什么我的tkinter gui没有显示一行并且需要很长时间才能加载?

提问于
浏览
0

美好的一天

我正在请求下面的“Python Tkinter Gui”的帮助 .

我希望程序从文本文件中读取,并在Tkinter Gui中输出一个折线图 .

我已经观看了所有相关视频,并且在寻求建议之前尝试了多种变体 . 我希望图形能够被动画化(自动刷新自己<0,但是此时我会很高兴至少在图形上看到一条描述我当前数据并且有一个刷新按钮的停滞线 .

问题是,在缓慢加载后,gui出现,然后gui冻结,没有显示任何行 . 要清楚,图形显示在topframe tkinter画布中,用户看不到任何行都可以查看数据,gui最终会冻结 .

我已成功使用Matplotlib将文本文件作为数据框读取并打开没有Tkinter的标准图形 . 我还可以正确地使用多种其他方法打开文本文件 .

文本文件中的数据当前看起来像下面的输出,我希望线图显示X轴上的日期和时间以及Y轴上的事件 .

datetime       event
20160117161947,1
20160117161951,1
20160117162028,1
20160117162057,2
20160117162128,1
20160117162140,2
20160117162227,1
20160117162228,1
20160117162313,1
20160117162328,1
20160117162356,1
20160117162428,1
20160117162520,2
20160117162528,1
20160117162607,1
20160117162624,5
20160117162628,1
20160117162728,1

这是文本文件的另一个示例 . 我只是使用SED去除特殊字符和空格 .

datetime            event
2016-01-06 21:22:57 , 18
2016-01-06 21:23:08 , 2
2016-01-06 21:23:09 , 2
2016-01-06 21:23:10 , 1
2016-01-06 21:23:13 , 1
2016-01-06 21:23:31 , 3
2016-01-06 21:23:43 , 1
2016-01-06 21:24:05 , 3
2016-01-06 21:24:09 , 3
2016-01-06 21:24:13 , 1
2016-01-06 21:24:19 , 2
2016-01-06 21:24:21 , 2
2016-01-06 21:24:31 , 3
2016-01-06 21:24:43 , 1
2016-01-06 21:24:49 , 2
2016-01-06 21:24:50 , 17

这是tkinter代码 . 此代码用于创建图形,但不包括行或任何数据(日期,时间或事件) . 另外,正如我所提到的,加载需要很长时间 .

#!/usr/bin/python

# standard numpy and pandas
import numpy as np
import pandas as pd
from pandas import Series,DataFrame

#import Tkinter
from Tkinter import *
import Tkinter as tk
#for python 2.7 from Tkinter use ttk is below
import ttk

#import running shell scripts
import subprocess

# Run tkinter code in another thread
import threading

# import for console code
import os

# standard graph imports
import numpy as np
import pandas as pd
from pandas import Series,DataFrame
import matplotlib
import matplotlib as mpl
import matplotlib.pyplot as plt

#special imports matplotlib and tkinter related

matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg,  

NavigationToolbar2TkAgg
from matplotlib.figure import Figure

#livedata import
import time
import matplotlib.animation as animation
from matplotlib import style

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

f = Figure(figsize=(4,4), dpi=50)
a = f.add_subplot(111)


root = Tk()

topFrame = Frame(root)
topFrame.pack(expand=YES)

botFrame = Frame(root)
botFrame.pack(side = BOTTOM, expand=YES)


df = pd.read_table('../data/linedata/test.txt', sep=",")
df.columns = ['datetime', 'event']
xar = []
yar = []
for x in df:
   if x > 1:
      xar.append(df.datetime)
      yar.append(df.event)
a.clear()
a.plot(xar,yar)

canvas = FigureCanvasTkAgg(f, root)
canvas.show()
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)
toolbar = NavigationToolbar2TkAgg(canvas, root)
#toolbar.update()
#canvas.tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=1)

#left side rectangle placer
canvas = Canvas(botFrame, width = 150, height = 600)
canvas.pack(side = LEFT, expand=True)
canvas.create_rectangle(5,5,175,700, fill="white")



root.geometry("900x900")

#animation interval time and stuff
#ani = animation.FuncAnimation(f, animation, interval=1000)

root.mainloop()

有时我收到下面的错误,我也很好奇这意味着什么 .

usr/local/lib/python2.7/dist-packages/matplotlib-1.5.0_918.gb6a79ac-py2.7-linux-x86_64.egg/matplotlib/__init__.py:1355: UserWarning:  This call to matplotlib.use() has no effect
because the backend has already been chosen;
matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
or matplotlib.backends is imported for the first time.

  warnings.warn(_use_error_msg)
root@Ghostnetdeb:/home/jinverar/projects/honeynet/views# python basicdfline.py
/usr/local/lib/python2.7/dist-packages/matplotlib-1.5.0_918.gb6a79ac-py2.7-linux-x86_64.egg/matplotlib/__init__.py:1355: UserWarning:  This call to matplotlib.use() has no effect
because the backend has already been chosen;
matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
or matplotlib.backends is imported for the first time.

1 回答

  • 0

    我能够使用在主Gui内部使用的下面的代码来解决这个问题 . 我还创建了一个程序,通过按下gui按钮将数据写入文本文件 . 每次关闭并重新打开Gui时,图表都会刷新 . 有人可以帮助我设置动态图表,该图表在文本文件更新时始终会更新吗?

    #graph STATIC WORKING****DONT"MESS
    
    #style.use("ggplot")
    plt.style.use("seaborn-deep")
    
    df = pd.read_table('data/newdata/linedata.txt', sep=",")
    df.columns = ['datetime', 'event']
    
    plt = Figure(figsize=(5,4), dpi=40)
    plt.subplots_adjust(bottom = 0.06, top = 0.98, left = 0.04, right = 0.97)
    ax = plt.add_subplot(111)
    
    ax.clear()
    df.plot(kind='line', x='datetime', y='event', ax=ax)
    
    canvas = FigureCanvasTkAgg(plt, root)
    canvas.show()
    canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)
    toolbar = NavigationToolbar2TkAgg(canvas, root)
    

相关问题