【发布时间】: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