我编写了一个代码来打印与给定mac地址对应的IP地址。问题是IP在retrieve\u输入函数中。如何在retrieve\u input函数之外获取IP值? 这是我的代码。
from tkinter import *
import subprocess
window = Tk()
window.title("Welcome..")
a = str(subprocess.getoutput(["arp", "-a"]))
print(a)
text=a
def retrieve_input(): #retrive input and fetches the IP
inputValue=txt.get(1.0, "end-1c")
ext = inputValue
if (inputValue in a and inputValue != ''):
nameOnly = text[:text.find(ext) + len(ext)]
ip = nameOnly.split()[-2]
print(ip)
window.geometry('900x700')
lbl1=Label(window, text="MAC ADDRESS",width=15,height=2,font=2)
lbl1.grid(column=0, row=0)
txt=Text(window, width=25,height=1)
txt.grid(column=1, row=0)
btn = Button(window, text="CONVERT",fg="black",bg="light
grey",width=15,font=4,height=2,command=lambda:(retrieve_input())
btn.grid(column=1, row=2)
window.mainloop()
如果不想使用全局变量,可以使用return IP,通过该函数可以返回想要使用的IP地址。你知道吗
但是,如果您了解Python类和属性,还有另一种方法。你知道吗
可以将
ip
设为如下全局变量:在您的代码中,它如下所示:
相关问题 更多 >
编程相关推荐