Reputation: 15
so im trying to make a script that you copy what you want then you run it and press the hotkey and it will open google and type "what is the meaning of (whatever word you copied) in Hebrew this is the code:
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()
and this is the error it prints out:
NameError: name 'search_google' is not defined
can someone plz help me figure this out becuse if i will do this part of the code
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())
on a new file it will work so plz help me figure this out ty
Upvotes: 0
Views: 1062
Reputation: 4427
Let's distill your question down to the parts that fail
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())
and then simplify/replace the portions that don't matter
def function_1():
def function_2():
pass
function_2()
as you can see from inspection as well as presumably in any error message you have, function_2
is no longer in scope by the time you call it. You will need to pull your function out into a higher scope or otherwise expose it in order to use it here:
def function_2():
pass
def function_1():
function_2()
function_2()
Upvotes: 2