首页 > 解决方案 > 将数据动态添加到 tkinter 表

问题描述

我想创建一个如下表

在此处输入图像描述

行中的数据是连续生成的。我想以动态创建行的方式创建我的表。我写了下面的代码(对 tinkter 来说非常新,可能只有 6 个小时新)但是没有插入数据。

from scapy.all import *
from scapy.layers.http import HTTPRequest,HTTPResponse,HTTP # import HTTP packet

from tkinter import ttk 
import tkinter as tk 


def generateData():
    sniff_packets()
    
    
def sniff_packets():
    window.mainloop() # <<---The window loop 
    window.after(300, process_packet)
    sniff(filter="port 80", prn=process_packet, iface="utun2", store=False)
    
    
def process_packet(packet):
    print("Called to process_packet() ")
    
    http_packet = str(packet)
    if packet.haslayer(HTTP):
        #if "www.xyz.com" in http_packet:
        #    print(http_packet)
        if 'XYZ' in http_packet:
            
            if HTTPRequest in packet:
                http_request = packet[HTTPRequest]
                insertDataDynamic((arrangePacket(str(http_request)))
                
               
                
            if HTTPResponse in packet:   
                http_response = packet[HTTPResponse]
                insertDataDynamic((arrangePacket(str(http_request)))
                
    
    
    
             
        
 
def insertDataDynamic(api_data):
        print("Called to insertDataDynamic() ")
        treev.insert("", 'end', text ="L1",  
             values =("DATA ", api_data, "HTTP"))        



    

def arrangePacket(httpLayer):
    
    ret = "***************************************GET PACKET****************************************************\n"
    ret += "\n".join(httpLayer.split(r"\r\n"))
    ret += "\n *****************************************************************************************************\n"
    return ret


if __name__ == "__main__":
    window = tk.Tk() 
    window.resizable(width = 1, height = 1) 
    treev = ttk.Treeview(window, selectmode ='browse') 
    treev.pack(side ='right') 
  
    # Constructing vertical scrollbar 
    # with treeview 
    verscrlbar = ttk.Scrollbar(window,  
                               orient ="vertical",  
                               command = treev.yview) 
      
    # Calling pack method w.r.to verical  
    # scrollbar 
    verscrlbar.pack(side ='right', fill ='x') 
      
    # Configuring treeview 
    treev.configure(xscrollcommand = verscrlbar.set) 
      
    # Defining number of columns 
    treev["columns"] = ("1","2","3") 
      
    # Defining heading 
    treev['show'] = 'headings'
      
    # Assigning the width and anchor to  the 
    # respective columns 
    treev.column("1", width = 500, anchor ='c') 
    treev.column("2", width = 500, anchor ='se') 
    treev.column("3", width = 500, anchor ='se')  
      
    # Assigning the heading names to the  
    # respective columns 
    treev.heading("1", text ="Name") 
    treev.heading("2", text ="Sex") 
    treev.heading("3", text ="Age")

    generateData()

同样,一旦 mainloop 启动,scapy 的 prn 功能就不起作用。

标签: pythontkinter

解决方案


我把你的函数放在了,mainloop所以当你的 gui 生成时它会被调用。另请注意,我将after()方法放入您的函数中,因此它将每 300 毫秒调用一次。

from tkinter import ttk 
import tkinter as tk 

treev = None
window = None

def generateData(self):
        #This is my API which makes a rest call and gets data 
        api_data = restcall()
        insertDataDynamic(api_data)
        window.after(300, generateData) 

def insertDataDynamic(self,api_data):
        treev.insert("", 'end', text ="L1",  
             values =(api_data.name, api_data.gender, api_data.age))  

if __name__ == "__main__":

    
    window = tk.Tk() 
    window.resizable(width = 1, height = 1) 
    treev = ttk.Treeview(window, selectmode ='browse') 
    treev.pack(side ='right') 
  
    # Constructing vertical scrollbar 
    # with treeview 
    verscrlbar = ttk.Scrollbar(window,  
                               orient ="vertical",  
                               command = treev.yview) 
      
    # Calling pack method w.r.to verical  
    # scrollbar 
    verscrlbar.pack(side ='right', fill ='x') 
      
    # Configuring treeview 
    treev.configure(xscrollcommand = verscrlbar.set) 
      
    # Defining number of columns 
    treev["columns"] = ("1","2","3") 
      
    # Defining heading 
    treev['show'] = 'headings'
      
    # Assigning the width and anchor to  the 
    # respective columns 
    treev.column("1", width = 500, anchor ='c') 
    treev.column("2", width = 500, anchor ='se') 
    treev.column("3", width = 500, anchor ='se')  
      
    # Assigning the heading names to the  
    # respective columns 
    treev.heading("1", text ="Name") 
    treev.heading("2", text ="Sex") 
    treev.heading("3", text ="Age")
    generateData()  
    window.mainloop()

可以在这里找到一个示例:

import tkinter as tk

def test():
    print('test')
    window.after(300, test)


window = tk.Tk()

test()
window.mainloop()

推荐阅读