Reputation: 37
I am making a tkinter program and for some reason [I have a function binded to control-O] when I use it, it creates a new line. Here is the function:
def fileOpen(textView):
try:
global currentText
global currentName
global currentTempName
myfile = tkFileDialog.askopenfile(title='Open a file', mode='r')
text.delete('1.0', END)
loadedfile = myfile.read()
currentText = loadedfile
currentFile = myfile.name
currentName = currentFile
currentName = currentName.rsplit('/', 1)[-1] #get the 'name.ext' part only
currentName = currentName.rsplit('\\', 1)[-1] #incase you're usin windows
currentTempName = currentName
currentFileButton.config(text = currentName)
myfile.close()
textView.insert("end", loadedfile)
except:
return
Here is where it's binded:
def ctrlO(arg):
fileOpen(text)
and the actual binding:
root.bind("<Control-o>", ctrlO)
[I am using a text widget, and binding to it doesn't fix the problem]
To better explain the problem, when I press ctrl+o to open the openfile dialog, it creates a new line, as if I pressed 'Enter', but only at the end of the file. I can provide more code if needed, but those are the only places those are used at.
Thanks
(I realize that function is messy)
Edit: It doesn't really affect the program as I clear the text between openings, but it just is a bit annoying and I'm sure easily fixable.
Upvotes: 0
Views: 328
Reputation: 9621
That's the normal behavior of Control-O in a Text widget; it would happen even if you didn't have your key binding at all. To override it, you need to return "break"
in your event handler, to prevent any further propagation of the event. I'm pretty sure that you'll have to bind directly to the Text widget for this to activate before the built-in behavior. You may still want a binding on the root widget, so that the command works even if the user never clicked in the text field to give it keyboard focus.
Upvotes: 1