Python 名称错误:名称';谷歌搜索';没有定义
因此,我试图制作一个脚本,你复制你想要的,然后你运行它,按下热键,它将打开谷歌,并键入“what is means of(what is the means of)(无论你复制了什么词)在希伯来语中的意思 代码如下:Python 名称错误:名称';谷歌搜索';没有定义,python,Python,因此,我试图制作一个脚本,你复制你想要的,然后你运行它,按下热键,它将打开谷歌,并键入“what is means of(what is the means of)(无论你复制了什么词)在希伯来语中的意思 代码如下: from pynput.keyboard import Key, KeyCode, Listener import webbrowser from googlesearch import search import pyperclip def function_1(): s
from pynput.keyboard import Key, KeyCode, Listener
import webbrowser
from googlesearch import search
import pyperclip
def function_1():
subject='hello'
def search_google(subject):
webbrowser.open("https://www.google.com/search?q=What is the meaning of "
+ subject
+ " in Hebrew")
search_google(pyperclip.paste())
#query='what is the mening of '+pyperclip.paste()+'in hebrew'
#for res in search(query, tld="co.in", num=10, stop=10, pause=2):
#webbrowser.open(res)
combination_to_function = {
frozenset([Key.delete, KeyCode(vk=67)]): function_1 # delete + c
}
pressed_vks = set()
def get_vk(key):
"""
Get the virtual key code from a key.
These are used so case/shift modifications are ignored.
"""
return key.vk if hasattr(key, 'vk') else key.value.vk
def is_combination_pressed(combination):
""" Check if a combination is satisfied using the keys pressed in pressed_vks """
return all([get_vk(key) in pressed_vks for key in combination])
def on_press(key):
""" When a key is pressed """
vk = get_vk(key) # Get the key's vk
pressed_vks.add(vk) # Add it to the set of currently pressed keys
for combination in combination_to_function: # Loop through each combination
if is_combination_pressed(combination): # Check if all keys in the combination are pressed
combination_to_function[combination]() # If so, execute the function
def on_release(key):
""" When a key is released """
vk = get_vk(key) # Get the key's vk
pressed_vks.remove(vk) # Remove it from the set of currently pressed keys
with Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
这是它打印出来的错误:
NameError: name 'search_google' is not defined
有人能帮我解决这个问题吗?如果我要做这部分代码的话
subject='hello'
def search_google(subject):
webbrowser.open("https://www.google.com/search?q=What is the meaning of "
+ subject
+ " in Hebrew")
search_google(pyperclip.paste())
在一个新的文件中,它会起作用,所以请帮助我解决这个问题。让我们把你的问题提炼到失败的部分
def function_1():
subject='hello'
def search_google(subject):
webbrowser.open("https://www.google.com/search?q=What is the meaning of "
+ subject
+ " in Hebrew")
search_google(pyperclip.paste())
然后简化/替换不重要的部分
def function_1():
def function_2():
pass
function_2()
正如您从检查以及可能在任何错误消息中看到的那样,function_2
在调用时已不在作用域中。您需要将函数拉出到更高的作用域中,或者以其他方式公开它,以便在此处使用它:
def function_2():
pass
def function_1():
function_2()
function_2()
让我们把你的问题归结到失败的部分
def function_1():
subject='hello'
def search_google(subject):
webbrowser.open("https://www.google.com/search?q=What is the meaning of "
+ subject
+ " in Hebrew")
search_google(pyperclip.paste())
然后简化/替换不重要的部分
def function_1():
def function_2():
pass
function_2()
正如您从检查以及可能在任何错误消息中看到的那样,function_2
在调用时已不在作用域中。您需要将函数拉出到更高的作用域中,或者以其他方式公开它,以便在此处使用它:
def function_2():
pass
def function_1():
function_2()
function_2()
您的函数
search\u google
位于function\u 1
中,只能从该函数中访问。请调用function\u 1()
或将search\u google
移动到外部。由于缩进错误,最后一个代码段无法独立工作。我可以在哪里调用函数\u 1?您的函数search\u google
位于函数\u 1
内部,只能从该函数内部访问。可以调用函数\u 1()
或将搜索\u google
移到外部。由于缩进错误,最后一个代码段无法独立工作。我在哪里可以调用函数\u 1?