Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8331815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:31:58+00:00 2026-06-09T02:31:58+00:00

I’m working on an interface using Tkinter and the canvas widget, and so far

  • 0

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.

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:

self.canvas.get_tk_widget().bind("<Control-o>",self.flash_open)

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

self.specfilename =askopenfilename(filetypes=[("spec", "")],initialdir= self.pathname)

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

def __init__(self):
    ...
    self.openfile() #calls the tkmessagebox
    self.root.mainloop() #starts gui

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

self.openfile()
self.root.mainloop()
self.root.focus_set()

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.

Edit:

I’ve written up a runable code segment to show exactly what my issue is.

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("<Control-o>",self.flash_do_work)
        self.root.bind("<Control-O>",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()

The bind will work if self.openfile() is removed from __init__, and used only from the menu

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T02:32:01+00:00Added an answer on June 9, 2026 at 2:32 am

    Change

    self.openfile()
    

    to

    self.root.after(1, self.openfile)
    

    This moves the call to askopenfilename into the main event loop. Having it outside the main event loop is somehow clobbering your event bindings.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
I have a view passing on information from a database: def serve_article(request, id): served_article
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.