1

I wanted to create a table like below

enter image description here

The data from the rows is generated continuously . I want to create my table in such a way that rows are created dynamically . I wrote below code (Very new to tinkter, may be just 6 hours new) but the no data is inserted .

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()

Also as soon as the mainloop starts ,the prn function of scapy doesn't work .

arpit joshi
  • 1,987
  • 8
  • 36
  • 62

2 Answers2

2

I put your function in the mainloop so it will be called when your gui is generated. Also note that I put the after() method in your function so it will call itself every 300 ms.

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()

an exampel can be found here:

import tkinter as tk

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


window = tk.Tk()

test()
window.mainloop()
martineau
  • 119,623
  • 25
  • 170
  • 301
Thingamabobs
  • 7,274
  • 5
  • 21
  • 54
  • I have modified my code with the actual code which generates the data . The sniff generated packets and I want to add packetInformation to tinker rows . The windowloop causes the prn of sniff process_packet to not be called . – arpit joshi Jul 06 '20 at 02:44
  • Are you sure that the mainloop causes the problem and not the after method? Because your programm did what it should do, just not automatically. Maybe you need to give your function more time to get done. Try 1000 instead of 300ms – Thingamabobs Jul 06 '20 at 03:10
  • But seems because of the mainloop the process_packet() is not called at all . If i remove mainloop then process_packet() is called by sniff . Not sure is it because both are continous process ? – arpit joshi Jul 06 '20 at 03:18
  • Oh you updated your question. I see. The thing is tkinter reads your code to the point the mainloop is called. So the mainloop should be the last readed line in your code, everything behind the mainloop will just be executed after you break your mainloop. – Thingamabobs Jul 06 '20 at 03:22
  • And thing is as soon as the mainloop is executed ,then the other part of code doesn't execute . The processPacket is continously called from sniff . So as soon I see a packet I want to show it in tkinter . – arpit joshi Jul 06 '20 at 03:26
  • Then you might be needed to ask another question maybe about subprocess. https://stackoverflow.com/questions/4417546/constantly-print-subprocess-output-while-process-is-running – Thingamabobs Jul 06 '20 at 03:32
0

window.mainloop() will not end until you close the window. It allows to expose the GUI and process events. There are several questions related to that topic. This one for instance: When do I need to call mainloop in a Tkinter application?

Eric Mathieu
  • 631
  • 6
  • 11