Note that there are some explanatory texts on larger screens.

plurals
  1. POTkinter Keyboard Binds
    primarykey
    data
    text
    <p>I'm working on an interface using Tkinter and the canvas widget, and so far have found answers to issues I have had from others questions and the answers posted, but I am stumped on this one. </p> <p>I have several keyboard binds in the class where my GUI elements are created, and they all work fine when the program is started. The binds looks something like this:</p> <pre><code>self.canvas.get_tk_widget().bind("&lt;Control-o&gt;",self.flash_open) </code></pre> <p>and are within the __init__ function of the class. As of yesterday, I initialized this class to start the program, then waited for the user to select open from a menu, which then opened (among other things) a tkmessagebox</p> <pre><code>self.specfilename =askopenfilename(filetypes=[("spec", "")],initialdir= self.pathname) </code></pre> <p>With this filename I am able to retrieve my required variable names from a certain filetype (inconsequential to the problem). Today I modified the __init__ function to call the open function when the program starts. Since nothing else can be done until this file is opened, it would make sense to open it first thing. Once the file is selected and the Tkmessagebox is closed, the root window is active, but none of the keyboard binds work. My functions still work using the menu/buttons assigned to them, just not the binds. I have tried binding the shortcuts to the root, with the same result, and am now thinking it may be an issue with the order I am calling them</p> <pre><code>def __init__(self): ... self.openfile() #calls the tkmessagebox self.root.mainloop() #starts gui </code></pre> <p>I had actually run into this issue before, where a toplevel() instance was closed/destroyed and disabled the binds of the parent window. There isn't any error message to speak of, the binds just don't do anything. I should also mention I have tried to focus on the root window again using </p> <pre><code>self.openfile() self.root.mainloop() self.root.focus_set() </code></pre> <p>I got around it before by using the wm_withdraw() and wm_deiconify() functions to simply hide the child window, then close it after the program is complete. This fix is a little more difficult to apply in this case however. If anyone can shed some light on the cause of the problem I'd appreciate it. </p> <p>Edit:</p> <p>I've written up a runable code segment to show exactly what my issue is. </p> <pre><code>import os from tkFileDialog import askopenfilename from Tkinter import * class Start: def __init__(self): self.root = Tk() self.root.title('Binding Troubles') menubar = Menu(self.root) #add items and their commands to the menubar filemenu = Menu(menubar, tearoff=0) filemenu.add_command(label="Do work", command=self.do_work) filemenu.add_command(label="Open File",command=self.openfile) menubar.add_cascade(label="File", menu=filemenu) #bind control-o to perform the do work function self.root.bind("&lt;Control-o&gt;",self.flash_do_work) self.root.bind("&lt;Control-O&gt;",self.flash_do_work) #add the menubar to the GUI self.root.config(menu=menubar) #initially open a tkdialog to open a file self.openfile()#comment out this line to make the bind work self.root.focus()#also tried self.root.focus_set() self.root.mainloop() def flash_do_work(self,event): #indirect tie to the do_work() function, I'm don't know a #proper way to make functions handle calls from both events and non-events self.do_work() def openfile(self): #gets current path self.pathname = os.getcwd() #Requests filename using a tkdialog self.filename =askopenfilename(initialdir= self.pathname) print self.filename def do_work(self): #placeholder for actual function; shows whether the bind is working or not print "work" Start() </code></pre> <p>The bind will work if self.openfile() is removed from __init__, and used only from the menu</p> <p>Another Edit: I've updated the example again, giving a menu option to run the openfile() function. I noticed that if openfile() is called in __init__, the bind will not work. But if next the openfile function is called again, this time manually from the menu, the bind will start working again. Not exactly sure what to take from this. Also, my apologies for the post getting so long.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload