[英]Adding values into Combobox tkinter from another function
I want to construct a combobox whose items are the IP addresses in local.network.我想构造一个combobox,它的项目是local.network中的IP地址。 NOTE: I am Linux user注意:我是 Linux 用户
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.当我运行上述代码时,我的 combobox 是空的。 Why couldn't I add gotten IP addresses into my combobox?为什么我不能将获得的 IP 地址添加到我的 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.我在控制台看到IP地址,取IP没有问题。
Briefly:简要地:
When you assign values to the combobox it doesn't keep a reference to the original data structure.当您为 combobox 赋值时,它不会保留对原始数据结构的引用。 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.所以在更新数据结构的时候你需要同时更新combobox。我将在下面以你的一些代码为例进行演示。
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.如果运行上面的代码,您将看到将 combobox 值设置到列表中,然后删除一半的值,而 combobox 的内容没有改变。 You have to exlicitly set the updated list of elements to be the combobox values.您必须明确地将更新的元素列表设置为 combobox 值。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.