Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 函数值检索:如何访问函数外部变量的值。如何在另一个函数中使用该值?_Python_Tkinter - Fatal编程技术网

Python 函数值检索:如何访问函数外部变量的值。如何在另一个函数中使用该值?

Python 函数值检索:如何访问函数外部变量的值。如何在另一个函数中使用该值?,python,tkinter,Python,Tkinter,我已经编写了一个代码来打印与给定mac地址对应的IP地址。问题是IP在retrieve_输入函数中。如何在retrieve_输入函数之外获取IP值? 这是我的代码。 from tkinter import * import subprocess window = Tk() window.title("Welcome..") a = str(subprocess.getoutput(["arp", "-a"])) print(a) text=a def retrieve_input():

我已经编写了一个代码来打印与给定mac地址对应的IP地址。问题是IP在retrieve_输入函数中。如何在retrieve_输入函数之外获取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()
如果您不想使用全局变量,可以使用returnip,通过该函数可以返回您想要使用的IP地址

但是,如果您了解Python类和属性,还有另一种方法

class IpValues:

    def __init__ (self):
        # Initialize and use it as constructor
        self.ip = None
        pass

    def retrieve_input(self):                   
        # 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)]
            self.ip = nameOnly.split()[-2]


ip_values_object = IpValues()
ip_values_object.retrieve_input()
print(ip_values_object.ip)

您可以将
ip
设置为如下全局变量:

def retrieve_input():
  global ip #declare ip as a global variable                   
  ip = "0.0.0.0" #assign a value to ip

retrieve_input()
print(ip) #access the value of ip outside the function
def retrieve_input():
    global ip #declare ip as a global variable
    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] #assign a value to ip
        print(ip)
在您的代码中,它将如下所示:

def retrieve_input():
  global ip #declare ip as a global variable                   
  ip = "0.0.0.0" #assign a value to ip

retrieve_input()
print(ip) #access the value of ip outside the function
def retrieve_input():
    global ip #declare ip as a global variable
    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] #assign a value to ip
        print(ip)

呃,使用
globals
?globals不起作用。在第一种情况下,我可以在retrieve\u输入函数之外获取ip值吗?假设我还有一个函数,我可以使用相同的ip值吗?好的,我将修改我的答案,使用return函数可以在函数之外获取变量,不过,其他答案对您来说更合适。我在第一个代码的末尾添加了一行,您可以看到,您可以从函数返回值,将其存储在“ip_值”上,您可以在函数外部使用它。我得到的错误是NameError:name“txt”没有定义,因为我试图打印ip_值。inputValue=txt.get(1.0,“end-1c”)有什么问题吗?但是ip的值在if循环中,对吗?我可以直接在if循环之外访问它吗?只要将它声明为全局变量,就可以从代码中的任何位置访问
ip
。看我的编辑,我试过了。但我得到的错误是打印(ip)名称错误:名称“ip”未定义def retrieve_input():全局ip inputValue=txt.get(1.0,“end-1c”)ext=inputValue if(a中的inputValue和inputValue!=''):canvas.config(background=“green”)name only=text[:text.find(ext)+len(ext)]ip=nameOnly.split()[-2]打印(ip)打印(“****************************************”)检索输入()打印(ip)这里我得到一个错误,因为名称错误:未定义名称“txt”