Python中的Tkinter Entry小部件是不可编辑的

dds*_*ard 5 python filedialog tkinter tkinter-entry

当我运行此代码时,文件选择器出现,然后当我完成它时,我无法输入条目小部件,直到我专注于另一个窗口然后回来.为什么会这样?

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()
Run Code Online (Sandbox Code Playgroud)

Kyr*_*bas -1

这应该可以修复它

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()
Run Code Online (Sandbox Code Playgroud)

通过运行askopenfilename第一个可以避免这个问题。

通过这种方式,您需要创建一个root窗口并将其撤回,否则您将得到两个窗口。

通过使用focus_setfocus_force您可以使盒子立即可供使用。