【问题标题】:Python - I'm declaring two arguments, but 1 is missing?Python - 我声明了两个参数,但缺少 1?
【发布时间】:2021-01-09 02:13:22
【问题描述】:

我正在尝试开发一个用单词替换用户定义的快捷方式的文本编辑器。我已经能够解决替换功能并将其调整为我的设计,但我无法使用控制台的语法(请参见下面的代码)并将信息提供给父窗口。但是,我一直遇到这个错误,我已经研究了几天来解决这个问题。它一直说“replace_shortcut() 缺少 1 个必需的位置参数:'shortcut'”,但我不确定我应该做什么。我已经查看了关于 SO 的其他问题,但没有与我的问题相关的内容。 (如果对我是 Python 新手有帮助,请从 C/C++ 切换)

代码:

from tkinter import *

# For the window
root = Tk()
root.title("Scrypt")

# For the parent text
text = Text(root)
text.pack(expand=1, fill=BOTH)

# For console window and text
win1 = Toplevel()
console_text = Text(win1, width=25, height=25)
console_text.pack(expand=1, fill=BOTH)

# For menubar
menubar = Menu(root)
root.config(menu=menubar)

def get_console_text(*args):
    syntax = console_text.get('1.0', 'end-1c')
    tokens = syntax.split(" ")
    word = tokens[:1]
    shortcut = tokens[2:3]
    # return word, shortcut

def replace_shortcut(word, shortcut):
    idx = '1.0'
    while 1:
        idx = text.search(shortcut, idx, stopindex=END)
        if not idx: break

        lastidx = '% s+% dc' % (idx, len(shortcut))

        text.delete(idx, lastidx)
        text.insert(idx, word)

        lastidx = '% s+% dc' % (idx, len(word))
        idx = lastidx

def open_console(*args):
    replace_shortcut(get_console_text())
    win1.mainloop()

# Function calls and key bindings
text.bind("<space>", replace_shortcut) and text.bind("<Return>", replace_shortcut)
win1.bind("<Return>", open_console)

# Menu bar
menubar.add_command(label="Shortcuts", command=open_console)

root.mainloop()

回溯(我想这就是所谓的):

replace_shortcut() missing 1 required positional argument: 'shortcut'
Stack trace:
 >  File "C:\Users\Keaton Lee\source\repos\PyTutorial\PyTutorial\PyTutorial.py", line 42, in open_console
 >    replace_shortcut(get_console_text())
 >  File "C:\Users\Keaton Lee\source\repos\PyTutorial\PyTutorial\PyTutorial.py", line 54, in <module>
 >    root.mainloop()

我不确定我是否遗漏了需要声明的第二个声明,但是感谢你们提供的任何帮助!

【问题讨论】:

  • 正如它所说,它还需要一个参数。位置意味着您必须专门提供它,即replace_shortcut(some_args, shortcut=your_shortcut_variable)
  • @kwkt 由于快捷方式是位置的,所以您不需要 shortcut= 部分 - replace_shortcut(some_args, your_shortcut_variable) 就足够了。
  • @TonySuffolk66 看来我把事情搞混了。谢谢!

标签: python desktop-application procedural-programming


【解决方案1】:

你的函数get_console_text()实际上并没有返回任何东西,所以当你调用replace_shortcut(get_console_text())时,你实际上是在调用replace_shortcut(None)

注意get_console_text() 函数中的行是:

def get_console_text(*args):
    syntax = console_text.get('1.0', 'end-1c')
    tokens = syntax.split(" ")
    word = tokens[:1]
    shortcut = tokens[2:3]
    # return word, shortcut   <------ you have commented out the return !!!!

你还需要这样做:

replace_shortcut(*get_console_text())

注意*,它告诉Python get_console_text 的返回值需要解压缩为两个参数,然后才能设置为replace_shortcut 作为两个参数。

【讨论】:

  • 即使这样它也不起作用,因为该函数将返回一个元组,而不是两个变量。您需要将参数解压缩到函数调用中,即replace_shortcut(*get_console_text())
  • 感谢托尼·萨福克 66 年向我展示!我完全忘记了我已经注释掉了那行。我尝试按照 Ted Klein Bergman 的建议从函数中解压缩参数,但我仍然遇到同样的错误。我应该在替换快捷方式末尾返回单词和快捷方式,还是会使代码有点臃肿?
  • @bluthhndr 返回这两个值并解包应该可以解决错误。如果您仍然收到错误,那么您没有正确实现它,或者您在其他地方有类似的错误。确保确认行号并确保您正在运行正确的文件(并且在运行之前已将其与所有更改一起保存)
【解决方案2】:

正如其他答案和评论指出的那样,原因很清楚。 还有一点: get_console_text() 将返回一个对象 None,因此投诉是“缺少 1 个必需的位置参数:'shortcut'”,而不是两个参数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多