Home > Enterprise >  Adding values into Combobox tkinter from another function
Adding values into Combobox tkinter from another function

Time:01-31

I want to construct a combobox whose items are the IP addresses in local network. NOTE: I am Linux user

from scapy.all import *
import sys
import os
from tkinter import *
from tkinter import ttk
import subprocess


class Uygulama(object):
    
    new_list1=None

    def __init__(self):
        self.araclar() #contain widgets and tools about windows
        self.refresh() #finding ip addresses in local network using scapy
    def refresh(self):
    
        self.new_list = []
        self.a, self.b = srp(Ether(dst="FF:FF:FF:FF:FF:FF") / ARP(pdst=self.ip_range2), timeout=2, 
       
        iface="eth0",retry=3)
             
        for self.snd, self.rcv in self.a:
            self.ip = self.rcv[ARP].psrc
            self.new_list.append(self.ip)
        self.new_list1 = self.new_list

     def araclar(self):
         self.ip_list1 = ttk.Combobox(width=27)
         self.ip_list1["values"] = self.new_list1
         self.ip_list1.place(relx=0.05, rely=0.08)
         self.ip_list1.set("Victim IP Address")

         self.dugme4 = Button(text="Refresh IPS", command=self.refresh, fg="black", bg="blue", font="bold")
         self.dugme4.place(relx=0.40, rely=0.18)

pencere = Tk()
uyg = Uygulama()
mainloop()   

When I run the foregoing code , my combobox seen empty. Why couldn't I add gotten IP addresses into my combobox? Can you help me?

NOTE: When I write:

def refresh(self):     
    self.new_list=[]
    self.a, self.b = srp(Ether(dst="FF:FF:FF:FF:FF:FF") / 
    ARP(pdst="192.168.1.0/24"), timeout=2, 
    iface="eth0",retry=3)
         
    for self.snd, self.rcv in self.a:
        self.ip = self.rcv[ARP].psrc
        self.new_list.append(self.ip)
    print(self.new_list1 = self.new_list)

I see the IP addresses in my console , there is no problem in taking IPs.

Briefly :

  • How can I add taken IPs in Combobox in tkinter?

CodePudding user response:

When you assign values to the combobox it doesn't keep a reference to the original data structure. So when updating the data structure you need to also update the combobox. I will demonstrate below using some of your code as an example.

class Uygulama(object):

    new_list1 = []

    def __init__(self):
        self.araclar() #contain widgets and tools about windows
        self.refresh() #finding ip addresses in local network using scapy

    def refresh(self):
        for i in range(25):
            self.new_list1.append(str(i))   # adding these to the list 
                                            #doesn't add them to the combobox
        print(self.new_list1)  # -> ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24']

        self.ip_list1["values"] = self.new_list1   # this adds them to the combobox
      

        for i in range(12):          # if I remove a bunch of items from the list
            self.new_list1.pop(0)    # it won't change the contents of the combobox
        print(self.new_list1)  # -> ['12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24']


    def araclar(self):
        self.ip_list1 = ttk.Combobox(width=27)
        self.ip_list1["values"] = self.new_list1  # the combo box won't know if you add values to the list
        self.ip_list1.place(relx=0.05, rely=0.08)
        self.ip_list1.set("Victim IP Address")

        self.dugme4 = Button(text="Refresh IPS", command=self.refresh, fg="black", bg="blue", font="bold")
        self.dugme4.place(relx=0.40, rely=0.18)

If you run the code above you will see that set the combobox values to the list, then remove half of the values and the combobox contents doesn't change. You have to exlicitly set the updated list of elements to be the combobox values.

So for your code you could probably just do this:

        for self.snd, self.rcv in self.a:
            self.ip = self.rcv[ARP].psrc
            self.new_list.append(self.ip)
        self.ip_list1["values"] = self.new_list1
  • Related