Keyboard shortcuts giving me errors in tkinter

#1
I am trying to create a text editor with python 3 and tkinter. The text editor works great except for when I try to use my keyboard shortcuts. Whenever I use any of the shortcuts, I get an error that says this:
Mã:
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 1699, in __call__
    return self.func(*args)
TypeError: newFile() takes 0 positional arguments but 1 was given
newFile() can be replaced with copySelected(), selectAll(), or whatever command I am trying to use. This only happens when I am trying to use the key bindings. It works just fine from the menu bar. The wierd thing is that when I am cutting, copying, or pasting I get the error they actually work in the app. Here is the key binding code:
Mã:
textField.bind("<Command-n>", newFile)
textField.bind("<Command-N>", newFile)
textField.bind("<Command-o>", openFile)
textField.bind("<Command-O>", openFile)
textField.bind("<Command-s>", saveFile)
textField.bind("<Command-S>", saveFile)
textField.bind("<Command-n>", newFile)
textField.bind("<Command-n>", newFile)
textField.bind("<Command-z>", undo)
textField.bind("<Command-Z>", undo)
textField.bind("<Command-Shift-z>", redo)
textField.bind("<Command-Shift-Z>", redo)
textField.bind("<Command-x>", cutSelected)
textField.bind("<Command-X>", cutSelected)
textField.bind("<Command-c>", copySelected)
textField.bind("<Command-C>", copySelected)
textField.bind("<Command-v>", paste)
textField.bind("<Command-V>", paste)
textField.bind("<Command-a>", selectAll)
textField.bind("<Command-A>", selectAll)
I am currently testing the code on Mac OS but I have already made the code os specific so that it will work on Windows and Linux as well. The Windows and Linux code is exactly the same other than the fact that Command is replaced with Control. The error occurs on all three of the platforms.

Any help is greatly appreciated. Thanks!
 

Admin

Administrator
Thành viên BQT
#2
When you bind a key to the function, tkinter will automatically pass an object to the callback. This object represents the event that caused the callback to be called. It has information such as the widget that received the event, the x and y coordinate of the mouse, and other details unique to the event (mouse button, keyboard character, etc).

When you bind a function to an event, your function must be able to accept this parameter. For example:

Mã:
def newFile(event):
    ...
Note that this is different than if you call the function via the command attribute of a widget. In that case no event object is passed. If you want to be able to call the function both via a binding and via a command attribute then you can make the parameter optional (and make sure that your function doesn't actually attempt to use it, since it may not be present):
Mã:
def newFile(event=None):
    ...
 
OP
OP
S

Shock9616

New Member
#3
Thank you so much! It works perfectly now! I used event=None since I need it to be a command and an event.
 

Từ khóa phổ biến

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top