how do I get keyboard to implement arguments correctly?

58 Views Asked by At

I keep getting the error: TypeError: runcmd() missing 1 required positional argument: 'inp' while trying to use keyboard.add_hotkey,

def runcmd(inp):
    text = inp
    kb.remove_hotkey("enter")
    if text == "help":
        from tkinter import messagebox
        messagebox.showinfo("commands","switchto <file> | opens <file> in the main editor\n")

def open_cmd_pallete():
    top= tk.Toplevel(root)
    top.geometry("750x30")
    top.title("command pallete")
    inp = tk.Entry(top)
    inp.pack()
    kb.add_hotkey("enter",runcmd,args=(inp.get())) # issue here

and whenever I try to change it to kb.add_hotkey("enter",runcmd(inp.get())) then it throws another error, so how do i fix this?

2

There are 2 best solutions below

1
On

for anyone who might (although very rare) come back to this. All you need to do is change the variable to a global, so in wherever (def), just change it to

def foo1():
    var1 = inp

def foo2():
    global var1
    ...

and it works

0
On

First you need to pass a tuple to args= instead. Second you should pass the reference of inp instead of inp.get() because there is nothing input when kb.add_hotkey("enter",runcmd,args=(inp.get())) is executed

Updated code:

def runcmd(inp):
    text = inp.get()
    ...

def open_cmd_pallete():
    ...
    kb.add_hotkey("enter", runcmd, args=(inp,)) # used tuple