5

When I run this code, the file picker comes up, and then when I finish with it, I can't type in the entry widget until I focus on another window and then come back. Why is this happening?

import tkinter as tk
from tkinter.filedialog import askopenfilename


location = ''
start = tk.Tk()

tk.Label(text='What is the name of your table?').pack()
box = tk.Entry(start, exportselection=0, state=tk.DISABLED)
box.pack()
button = tk.Button(start, text='OK', command=lambda e: None)
button.pack()
location = askopenfilename(defaultextension='.db', 
                           title="Choose your database", 
                           filetypes=[('Database Files', '.db'), ('All files', '*')])
box.config(state=tk.NORMAL)

start.mainloop()
Cœur
  • 37,241
  • 25
  • 195
  • 267
ddsnowboard
  • 922
  • 5
  • 13
  • Are you running this on OSX? – Bryan Oakley Aug 03 '14 at 18:48
  • I don't have a clue why, but I suppose you could do the askopenfilename first then mainloop start... – W1ll1amvl Aug 04 '14 at 05:28
  • I'm not on OSX, I'm on Windows 7 & 8.1. And how could I make the mainloop not start until the askopenfilename window is closed? – ddsnowboard Aug 04 '14 at 16:44
  • To not run the mainloop until the askopenfilename is closed is nearly impossible as it always needs a root or primary window so you could possibly create a temp one and do root.withdraw() – Dan Alexander Sep 21 '14 at 02:09
  • The entry widget thing could possibly be a bug within Python 2 so I would consider updating to Python 3 and using that tkinter in there. – Dan Alexander Sep 21 '14 at 02:11
  • @ddsnowboard did you find a solution to the above issue. I am currently working on something similar, and this is where i am stuck. – saurav Aug 07 '18 at 10:44

3 Answers3

2

You just write box.focus_force() below box.pack() and that should do the work for you.

saurav
  • 427
  • 4
  • 14
0

You need to follow 2 steps:

  1. Write box.focus_force() below box.pack().
  2. Cut the code location = askopenfilename(blah blah..) and paste it after start = tk.Tk()

You will be good to go!

-1

This should fix it

import tkinter as tk
from tkinter.filedialog import askopenfilename
location = ''
root = tk.Tk()
root.withdraw()
location = askopenfilename(defaultextension='.db', title="Choose your database", filetypes=[('Database Files', '.db'), ('All files', '*')])
start = tk.Tk()
tk.Label(start, text='What is the name of your table?').pack()
box = tk.Entry(start, exportselection=0, state=tk.DISABLED)
box.pack()
start.focus_set()
box.focus_set()
start.focus_force()
button = tk.Button(start, text='OK', command=lambda e: None)
button.pack()
box.config(state=tk.NORMAL)
start.mainloop()

By running the askopenfilename first you can avoid this issue.

In doing it this way you need to make a root window and withdraw it or you will get two windows.

By using focus_set and focus_force you can make the box immediately ready to use.

Kyrubas
  • 877
  • 8
  • 23
  • This did not solve the issue. I am working on something similar and this is still giving trouble. You have any other idea? – saurav Aug 07 '18 at 10:43