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

  • SEARCH
  • Home
  • 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 8981793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:25:26+00:00 2026-06-15T20:25:26+00:00

I have a (second) tkinter window, which, when opened, does not get the focus,

  • 0

I have a (second) tkinter window, which, when opened, does not get the focus, but rather the first window remains focused (although the second window appears in front of the other).
It contains a textbox which I want to be able to type in, but I have to double-click it in order to type.

How do I focus the textbox when opening the window?

My tries:
textbox.focus_set(),
window.grab_set(),
window.focus_set()

None of them did what I wanted to do.

EDIT:
Instead, .focus_set() raises an error when (and only when) closing the main window: can't invoke "focus" command: application has been destroyed

This is my current code (tkWin is the main window, tkcWinis the second window):

def click(self, field):
    import _tkinter
    if field != None:
        try:
            self.tkcWin = Tk()#creating window
            self.tkcWin.focus()
            self.tkcWin.title(field)
            self.tkcWin.geometry('300x100')
            self.mainframe = Frame(master=self.tkcWin,background="#60BF98")
            self.mainframe.place(x=0, y=0, width=300, height=300)
            self.textb = Text(master=self.mainframe)
            self.textb.place(x=0, y=50)
            self.textb.bind("<Return>",lambda a: self.setM(field))
            self.textb.bind("<Return>",lambda a: self.tkcWin.destroy(),True)
            self.tkcWin.grab_set()
            self.tkWin.wait_window(self.tkcWin)
            self.textb.focus_set()
            hwnd = self.tkcWin.winfo_id()
            ctypes.windll.user32.SetFocus(hwnd)
            self.tkcWin.mainloop()
        except _tkinter.TclError:
            self.tkcWin.destroy()
  • 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-15T20:25:27+00:00Added an answer on June 15, 2026 at 8:25 pm

    It turns out that you can simply call the secondary window’s deiconify() method and then the widget’s focus_set() method:

    toplevel.deiconify()
    text.focus_set()
    

    Here’s the original work-around for Windows (no longer recommended):

    Start by adding import ctypes at the top.
    Go ahead and focus your widget like you have with: text.focus_set()
    Get the hwnd of the second window: top_hwnd = toplevel.winfo_id()
    And finally activate the second window with: ctypes.windll.user32.SetFocus(top_hwnd)

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

Sidebar

Related Questions

I have a second window which opens when a certain staffname is searched for,
we want to have the second biggest element. We first use ANY to exclude
I have an Android MapView in which i need to have a second layer
I have a 30 second audio file and I would like to get callbacks
I have a second form on my page which I want to use the
is there any posibility to get a second tag (or something else which i
I have simple JSF application. Everything works fine, but not navigation. It behaves like
I have some working code that I altered but I can't seem to get
I have a second database in my Django application, which is filled with static
I have a Python script which uses Tkinter for the GUI. My little script

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.